Redux - 在组件

时间:2017-10-19 09:14:28

标签: javascript reactjs redux es6-promise

我有一个'addToFolder'动作,它会调度我的Redux减速器中处理的动作。

export function addToFolder(folderIds, contentId, contentType) {
  const item = {};
  item.folder_ids = folderIds;
  item.content_id = contentId;
  item.content_type = contentType;

  return function(dispatch) {
    return fetch(`${getAPIPath}api/addtofolder`, {
      method: 'PUT',
      body: JSON.stringify(item),
      credentials: 'include',
      headers: {
        Accept: 'application/json',
      },
    })
    .then((response) => {
      response.json().then((res) => {
        dispatch(addToFolderSuccess(res.data));
      });
    }).catch((error) => {
      throw (error);
    });
  };
}

点击我的“添加到文件夹”按钮后,在我的组件中,我发送了该动作。我想使用.then来触发小吃/通知操作,但是使用我的API调用的响应来填写通知显示的id和消息文本。目前response未定义。

addToFolder = () => {
    this.props.actions.addToFolder(this.state.activeItems, this.props.contentId, this.props.contentType)
      .then((response) => {
        this.setState({ addBtnLoading: false });
        this.props.actions.dispatch(showSnack(response.id, {
          label: response.message,
          timeout: 4000,
        }));
      });
  };

1 个答案:

答案 0 :(得分:1)

你忘了回来

.then((response) => {
      // nested promise
      return response.json().then((res) => {
        dispatch(addToFolderSuccess(res.data));

        // the result
        return res;
      });