varad
varad

Reputation: 8029

redux reducer returned undefined handling

My reducers like:

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
  }
}

Here I am getting action object with fname and lname but this is giving me error saying .. Uncaught Error: Reducer "login" returned undefined handling "LOGIN". To ignore an action, you must explicitly return the previous state.

Why I am getting this error ?

Upvotes: 2

Views: 6119

Answers (3)

Ritwik
Ritwik

Reputation: 51

For me, it was happening because I forgot to make the action call asynchronous 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 });
};

Upvotes: 0

jayDawg
jayDawg

Reputation: 1

or

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

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

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

Upvotes: 0

Dmitry  Yaremenko
Dmitry Yaremenko

Reputation: 2570

Replace:

 return
 [

To:

 return [

Because first is like return; (js adds ; at the end of line)

Upvotes: 6

Related Questions