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

后端 未结 7 2257
死守一世寂寞
死守一世寂寞 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:58

    The initialState argument in createStore() is often tripping people. It was never meant as a way to “initialize” your application state manually. The only useful applications for it are:

    • Booting up server rendered app from JSON state payload.
    • “Resuming” the app from a state saved into local storage.

    It is implied that you never write initialState manually and in most apps you don’t even use it. Instead, reducers must always specify their own initial state, and initialState is just a way to prefill that state when you have an existing serialized version of it.

    So this answer is correct: you need to define the initial state in your reducer. Supplying it to createStore() is not enough, and is not meant to be a way to define the initial state in code.

    0 讨论(0)
提交回复
热议问题