【问题标题】:redux reducer returned undefined handlingredux reducer 返回未定义的处理
【发布时间】:2016-05-05 12:12:04
【问题描述】:

我的减速器喜欢:

const initialState = [
  {
    fname: null,
    lname: false,
  }
]

export default function login(state = initialState, action) {
  switch (action.type) {
    case LOGIN:
      console.log("actions")
      console.log(action)
      console.log("reducers")
        return 
        [{
          fname: action.fname,
          lname: action.lname,
          }]
    default:
      return state
  }
}

在这里,我得到了带有fnamelname 的操作对象,但这给了我一个错误说.. Uncaught Error: Reducer "login" returned undefined handling "LOGIN". To ignore an action, you must explicitly return the previous state.

为什么会出现这个错误?

【问题讨论】:

    标签: redux reducers


    【解决方案1】:

    对我来说,发生这种情况是因为我忘记使动作调用异步async

    export const asyncFetchUser = () => async (dispatch) => {
      const response = *await* axios.get('http://localhost:5000/api/current_user');
      dispatch({ type: ASYNC_FETCH_USER, auth: response.data });
    };
    

    【讨论】:

      【解决方案2】:

          ...
          console.log("reducers")
                  return([{
                       fname: action.fname,
                       lname: action.lname,
                    }])
      
        //example 
        function getPerson(){
          return([{
                    fname: 'jay',
                    lname: 'dawg'
                }])
        }
      
       console.log(getPerson()[0].lname) // "dawg"
      

      【讨论】:

        【解决方案3】:

        替换:

         return
         [
        

        收件人:

         return [
        

        因为first就像return;(js在行尾加上;

        【讨论】:

          猜你喜欢
          • 2018-05-27
          • 2019-07-09
          • 2022-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-31
          • 1970-01-01
          • 2021-07-02
          • 2017-05-16
          相关资源
          最近更新 更多