【问题标题】:Reducer not called after action return?动作返回后没有调用减速器?
【发布时间】:2020-02-07 20:38:49
【问题描述】:

这是我的登录操作代码。我究竟做错了什么 ?如您所见,reducer 状态更新未调用。

请帮帮我!

反应 - 16.8 Axios Http 客户端 Node & Mongo Db 后端

export const loginUser = (userData) => {
    axios.post(URL + '/api/admin/login', userData)
        .then(res => {
            return {
                type: SIGNIN_USER,
                payload: storeData
            }

        })
        .catch(err => {
            return {
                type: SHOW_MESSAGE,
                payload: err.response.data
            }
        });
};

【问题讨论】:

    标签: reactjs mongodb redux


    【解决方案1】:

    使用箭头函数可以提高代码的可读性。 API.fetchComments 中不需要返回任何内容,Api 调用是异步的,请求完成后会得到响应,你只需要调度类型和数据。

    下面的代码通过使用箭头函数来完成同样的工作。

    export const bindComments = postId => {
      return dispatch => {
        API.fetchComments(postId).then(comments => {
          dispatch({
            type: BIND_COMMENTS,
            comments,
            postId
          });
        });
      };
    }; 
    

    参考链接:React-Redux: Actions must be plain objects. Use custom middleware for async actions

    【讨论】:

      【解决方案2】:

      试试这个代码示例:

      export const loginUser = userData => dispatch => (
          axios.post(URL + '/api/admin/login', userData)
              .then(res => dispatch({ type: SIGNIN_USER, payload: res }))
              .catch(err => dispatch({ type: SHOW_MESSAGE, payload: err.response.data }))
      )
      

      【讨论】:

      • 我试过这个,但失败了。错误之类的动作必须是一个对象
      • 这意味着你没有以正确的方式从组件中分派函数。
      • 签出此链接并按照正确的方式调用组件中的操作。 medium.com/backticks-tildes/…
      【解决方案3】:

      您需要调度操作,而不仅仅是返回对象:

      const dispatch = useDispatch(); // Assuming you're inside functional component
      
      export const loginUser = (userData) => {
          axios.post(URL + '/api/admin/login', userData)
              .then(res => {
                  return dispatch({
                      type: SIGNIN_USER,
                      payload: storeData
                  })
      
              })
              .catch(err => {
                  return dispatch({
                      type: SHOW_MESSAGE,
                      payload: err.response.data
                  })
              });
      };
      

      【讨论】:

      • 返回dispatch结果的意义何在?它只是很少有用。
      • ./src/appRedux/actions/Auth.js 第 31 行:'useDispatch' is not defined no-undef
      • 你需要先做import { useDispatch } from 'react-redux'。更多信息:react-redux.js.org/next/api/hooks#usedispatch
      【解决方案4】:
          .then(res => {
              return {
                  type: SIGNIN_USER,
                  payload: storeData
              }
      
          })
      

      不要返回 res,而是在此处对其应用操作。你提到了改变状态,所以类似的东西:

          .then(res => {
              this.state.someResult = res;
          })
      

      【讨论】:

      • 不,我想从 dispatch action 中调用 reducer 函数
      猜你喜欢
      • 2017-09-26
      • 1970-01-01
      • 2017-03-12
      • 2015-01-11
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      相关资源
      最近更新 更多