Skip to content
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

Document required default state in reducer #1617

Closed
DaveStein opened this issue Apr 14, 2016 · 8 comments
Closed

Document required default state in reducer #1617

DaveStein opened this issue Apr 14, 2016 · 8 comments
Labels

Comments

@DaveStein
Copy link

Related to my pitfall here: http://stackoverflow.com/questions/36631728/why-isnt-combinereducers-receiving-state-in-redux/36633079#36633079

The docs show reducers having a default value for state, but it is never listed as required. The fact that an INIT action is fired before createStore takes advantage of initialState was confusing.

@gaearon
Copy link
Contributor

gaearon commented Apr 14, 2016

What can we do better? I think we’re already warning if you forgot to specify the initial state—is the warning not descriptive, or misleading?

@DaveStein
Copy link
Author

The warning simply tells me that nav is not returning state. I console logged the state at that point, being confused that I had passed initialState into createStore. It was being hit because of the INIT action from Redux, which passes state as undefined no matter what. So really the docs need to specify that default declaration is required when making a reducer (even if its just empty object, string, etc)

@markerikson
Copy link
Contributor

@DaveStein : you might want to read through http://stackoverflow.com/questions/33749759/read-stores-initial-state-in-redux-reducer , which covers how reducer default state and createStore initial state interact.

This probably should go in the FAQ. I think I may have skipped it because I was considering putting it in a "Structuring Reducers", page, which I decided not to do as part of the initial FAQ work.

@DaveStein
Copy link
Author

@markerikson I would agree :) Going through Stack to find answers is unexpected. I did get the actual help via the IRC for #reactjs. I know the pain of trying to get docs write for people while also working on the thing you are doc'ing. But at least noobs like me point out what might need improvement haha

@DaveStein
Copy link
Author

DaveStein commented Apr 14, 2016

I guess another source of confusion is that while the reducers section says "Redux will call our reducer with an undefined state for the first time", it is unclear that it maintains that even if createStore has the default state, which is optional. Stores come after that page so I assumed it would override that behavior. So my assumption was very wrong.

@gaearon
Copy link
Contributor

gaearon commented Apr 14, 2016

Yeah, initialState 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.

Maybe we should rename it to prefilledState or rehydratedState to make a point..

@gaearon
Copy link
Contributor

gaearon commented Apr 14, 2016

@markerikson
Copy link
Contributor

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

3 participants