【问题标题】:Promise not returning when using mapDispatchToProps使用 mapDispatchToProps 时承诺不返回
【发布时间】:2017-05-22 07:49:23
【问题描述】:

最近我已经从使用一个乐观操作转变为添加另外两个来检测成功/失败的服务器响应。

通过乐观的方法,我能够在我的行动中以速记方式和承诺链传递:

class Post extends Component {
  onUpdateClick(props) {
    this.props.updatePost(this.props.params.id, props)
      .then(() => /* Action goes here */);
  }
}

...

export default connect(mapStateToProps, { updatePost })(Post);

现在我正在调度多个操作并使用mapDispatchToProps,操作返回未定义。

Uncaught (in promise) TypeError: Cannot read property 'then' of undefined

这里发生了什么?请注意,我使用的是redux-promise。

function mapDispatchToProps(dispatch) {
  return {
    dispatch(updatePost(id, props))
      .then(result => {
        if (result.payload.response && result.payload.response.status !== 200) {
         dispatch(updatePostFailure(result.payload.response.data));
        } else {
         dispatch(updatePostSuccess(result.payload.data));
        }
      });
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(Post);

export function updatePost(id, props) {
  const request = axios.put(`${ROOT_URL}/posts/${id}`, props);

  return {
    type: UPDATE_POST,
    payload: request,
  };
}

export function updatePostSuccess(activePost) {
  return {
    type: UPDATE_POST_SUCCESS,
    payload: activePost,
  };
}

export function updatePostFailure(error) {
  return {
    type: UPDATE_POST_FAILURE,
    payload: error,
  };
}

const initialState = { 
  activePost: { post: null, error: null, loading: false },
};

export default function(state = initialState, action) {
  let error;

  switch (action.type) {
    case UPDATE_POST: {
      return { ...state, activePost: { ...state.post, loading: true, error: null } };
    }

    case UPDATE_POST_SUCCESS: {
      return { ...state, activePost: { post: action.payload, loading: false, error: null } };
    }

    case UPDATE_POST_FAILURE: {
      error = action.payload || { message: action.payload.message };

      return { ...state, activePost: { ...state.activePost, loading: false, error: error } };
    }
  }
}

【问题讨论】:

  • 我认为您必须使用 connect(mapStateToProps, { updatePost, updateAlbumType })(Post); 将您的 updateAlbumType 操作与 Post 道具映射起来
  • 抱歉,引用了不同的模型。那应该是updatePost。但在我的第二个示例中,它们应该通过我的 mapDispatchToProps 函数映射到 Post 道具:export default connect(mapStateToProps, mapDispatchToProps)(Post);

标签: javascript reactjs promise redux react-redux


【解决方案1】:

mapDispatchToProps 函数的语法似乎不正确。 它必须返回一个包含方法作为属性的对象。

试着写这样的东西:

function mapDispatchToProps(dispatch) {
  return {
    updatePost() {
      return dispatch(updatePost(id, props))
        .then(result => {
          if (result.payload.response && result.payload.response.status !== 200) {
           return dispatch(updatePostFailure(result.payload.response.data));
          }
          return dispatch(updatePostSuccess(result.payload.data));         
        });
    }
  }
}

【讨论】:

  • 非常感谢!这个答案来得正是时候。就一个问题。为什么一定要退货?调度程序内部的函数不应该在操作中返回对象吗?
  • 你必须返回dispatch,因为如果你什么都不返回,updatePost() 将被评估为undefined,而dispatch(...) 是一个承诺。
  • 明白了。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2018-02-15
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多