react redux异步哪种更合理?是正确姿势?优雅姿势?

我现在用的这种写法

export function getUserList(params) {

    return (dispatch) => {
        dispatch({'type': TYPES.LOADING});
        lendService.getUserList(params).then((data) => {
            dispatch({'type': TYPES.LOADING_STOP});
        }).catch((error) => {
            dispatch({'type': TYPES.LOADING_STOP});
            dispatch({'type': TYPES.SHOW_MESSAGE, text: error.message});
        });
    }
}

然后看到这种写法

export const getPostByCategory = createAction(
  types.FETCH_POSTS_BY_CATEGORY, 
  async(category, params)=> {
    return await postService.getPostByCategory(category, {
      pageIndex: 1
    });
  }, 
  (category)=> {
    return {
      pending: true,
      category
    }
  }
);

迷茫了,哪种是正确姿势?

阅读 2.9k
1 个回答

官方应该更推荐第一种,配合redux-thunk使用。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题