Read Store's Initial State in Redux Reducer

前端 未结 4 1955
慢半拍i
慢半拍i 2020-11-29 15:30

Initial state in a Redux app can be set in two ways:

  • pass it as the second argument to createStore (docs link)
  • pass it as the first argum
4条回答
  •  离开以前
    2020-11-29 15:43

    In a nutshell: it's Redux the one who passes the initial state to the reducers, you don't need to do anything.

    When you call createStore(reducer, [initialState]) you are letting Redux know what is the initial state to be passed to the reducer when the first action comes in.

    The second option you mention, applies only in case you didn't pass an initial state when creating the store. i.e.

    function todoApp(state = initialState, action)

    state will only be initialised if there was no state passed by Redux

提交回复
热议问题