Why do I get “Reducer […] returned undefined during initialization” despite providing initialState to createStore()?

后端 未结 7 2261
死守一世寂寞
死守一世寂寞 2020-12-13 08:27

I had set InitialState in my redux createStore method ,and I corresponding InitialState as second arguments

I got a error in browser:

Un         


        
7条回答
  •  感情败类
    2020-12-13 08:31

    as per the documentation of redux: https://redux.js.org/basics/reducers

    you need to return state out side of switch function like this:

    
     function posts(state=initialState,action) {
      switch (action.type) {
        case INVALIDATE_SUBREDDIT:
          return state.merge({
            didInvalidate: true
          })
        case REQUEST_POSTS:
          return state.merge({
            isFetching: true,
            didInvalidate: false
          })
        case RECEIVE_POSTS:
          return state.merge({
            isFetching: false,
            didInvalidate: false,
            items: action.posts,
            lastUpdated: action.receivedAt
          })
        default:
          return state 
        }
    
    //here you should put the return state
    return state
    }
    

提交回复
热议问题