Skip to content

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

Closed
mohsen1 opened this issue Feb 21, 2016 · 9 comments
Labels

Comments

@mohsen1
Copy link

mohsen1 commented Feb 21, 2016

Why assertReducerSanity in combineReducers passes undefined 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?

@mohsen1 mohsen1 closed this as completed Feb 21, 2016
@gaearon
Copy link
Contributor

gaearon commented Feb 21, 2016

In general we consider initialState argument to createStore an optimization. It is handy for persistence and rehydrating the store when rendering on the server, but your app is supposed to be able to “boot up” without that argument.

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 combineReducers().

@mohsen1
Copy link
Author

mohsen1 commented Feb 21, 2016

Thanks for explanation. Having an initial state in each reducer helped me re-use a reducer in my code actually!

https://github.com/contacts-mvc/redux-react/blob/9b98792f1b6d8a731f220fc8c66263c7a9611f81/reducers/selectedContactId.ts#L13

@pke
Copy link

pke commented Feb 22, 2016

Any sample code how reducers init themselves?

@sompylasar
Copy link

@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:
https://github.com/reactjs/redux/blob/master/examples/todomvc/reducers/todos.js#L11
https://github.com/reactjs/redux/blob/master/examples/todomvc/reducers/todos.js#L3-L9

@pke
Copy link

pke commented Feb 22, 2016

I see. I was merely using @gaearon words

your reducers can handle empty state and initialize themselves if necessary

How would a reducer initialize itself with a remote list of items?

@sompylasar
Copy link

@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.

@gaearon
Copy link
Contributor

gaearon commented Feb 22, 2016

Yep. I was referring to synchronous initialization, by which I meant that reducer should return some initial state if it receives undefined as first argument. Any further changes in state much come through actions.

@pke
Copy link

pke commented Feb 22, 2016

And the action to load the remote data should be initiated in the reducers default handler?

@gaearon
Copy link
Contributor

gaearon commented Feb 22, 2016

No, reducers cannot initiate actions. They are passive and only react to actions.
The actions can be initiated by components.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants