【问题标题】:Redux-saga add additional field for POST bodyRedux-saga 为 POST 正文添加附加字段
【发布时间】:2018-08-28 15:33:39
【问题描述】:

我正在尝试使用 redux-saga 发布请求。我有两个输入的形式。我正在使用选择器从表单中收集输入数据,现在它是我的身体。但是我需要添加 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,
    });
  }
}

【问题讨论】:

  • 使用 body.itemId = 设置任何值
  • @AmruthLS,你能给我看个小例子吗,因为我整天都在尝试这个。我正在使用选择器,并且有一个完整的 json 对象。
  • 我的意思是在这一行之后 const body = yield select(makeSelectModifiedData()); body.itemId =5 这样
  • @AmruthLS,它没有取值。
  • 如果不将const body更改为let body

标签: reactjs react-redux redux-saga


【解决方案1】:

将 itemId 附加到正文。

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

【讨论】:

    【解决方案2】:

    你可以这样做:

    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,
        });
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多