Redux-saga 为 POST 添加附加字段 body

Redux-saga add additional field for POST body

我正在尝试 POST 使用 redux-saga 请求。我有两个输入的形式。我正在使用选择器从表单中收集输入数据,现在它是我的 body。但是我需要添加不是用户输入的itemId。我应该如何将其插入 body?

我的故事是这样的:

export function* submitForm() {
  try {
    const formType = 'item';
    const body = yield select(makeSelectModifiedData());
    let requestURL;

    switch (formType) {
      case 'item':
        requestURL = 'http://localhost:1587/item';
        break;
      default:
    }

    const response = yield call(request, requestURL, { method: 'POST', body });

    if (response.itemId) {
      yield call(forwardTo, '/item');
    }
  } catch (error) {
    Alert.error('Error message...', {
      html: false,
    });
  }
}

你可以这样做:

export function* submitForm() {
  try {
    const formType = 'item';
    const myFormValues = yield select(makeSelectModifiedData());
    const body = {
      ...myFormValues,
      itemId: myItemId
    };
    let requestURL;

    switch (formType) {
      case 'item':
        requestURL = 'http://localhost:1587/item';
        break;
      default:
    }

    const response = yield call(request, requestURL, { method: 'POST', body });

    if (response.itemId) {
      yield call(forwardTo, '/item');
    }
  } catch (error) {
    Alert.error('Error message...', {
      html: false,
    });
  }
}

将 itemId 附加到正文。

    let body = yield select(makeSelectModifiedData());
    body.itemId = 5;//provide id here