redux reducer返回undefined处理

时间:2016-01-28 08:16:16

标签: redux reducers

我的减速机如:

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.

为什么我收到此错误?

3 个答案:

答案 0 :(得分:6)

替换:

 return
 [

要:

 return [

因为第一个就像return;(js在行尾添加;

答案 1 :(得分:0)

    ...
    console.log("reducers")
            return([{
                 fname: action.fname,
                 lname: action.lname,
              }])

  //example 
  function getPerson(){
    return([{
              fname: 'jay',
              lname: 'dawg'
          }])
  }

 console.log(getPerson()[0].lname) // "dawg"

答案 2 :(得分:0)

对我而言,这是因为我忘记了使操作调用异步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 });
};