-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
How to use createStore second argument for initial state with combined reducers? #1431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
In general we consider The reducer sanity check verifies that your reducers can handle empty state and initialize themselves if necessary. Preparing a single “initial state tree” outside the reducers is an anti-pattern because it makes them less reusable and easier to break during a refactoring. If you don’t agree with our opinion you can always write the root reducer by hand instead of using the stock |
Thanks for explanation. Having an initial state in each reducer helped me re-use a reducer in my code actually! |
Any sample code how reducers init themselves? |
@pke Reducers are stateless, they do not init themselves, rather they return the initial value of the piece of application state they are responsible for. If the state passed to the reducer is undefined, the initial value should be returned: |
I see. I was merely using @gaearon words
How would a reducer initialize itself with a remote list of items? |
@pke Any state change is done solely via actions, even the state initialization (Redux calls the reducer with an empty state and an INIT action to get the very first state from the reducer). The very first state can be empty if you have no data right away. Then, to change the state from empty to full (e.g. after asynchronously receiving data from remote location), you'd have an action with a payload required to fill the state. |
Yep. I was referring to synchronous initialization, by which I meant that reducer should return some initial state if it receives |
And the action to load the remote data should be initiated in the reducers |
No, reducers cannot initiate actions. They are passive and only react to actions. Check out http://redux.js.org/docs/advanced/AsyncActions.html. To be fair there are approaches where reducers initiate actions but they are experimental. See https://github.com/raisemarketplace/redux-loop for an example. |
Why
assertReducerSanity
incombineReducers
passesundefined
to reducers to check if they are "sane"?In my app I want to send the initial state when I'm making the store in the second argument of
createStore
?The text was updated successfully, but these errors were encountered: