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

Reducers (ex-'stores') must have a default return value for state to be initialized properly? #173

Closed
burmisov opened this issue Jun 23, 2015 · 5 comments

Comments

@burmisov
Copy link

Just spent some half an hour figuring this out.
Say I only had one action type to process in my reducer, so, based on my experience with 'vanilla flux', I did it like this:

export default function myReducer (state = initialState, action) {
  switch (action.type) {
   case THE_ONE_ACTION_I_LISTEN_TO:
     return { ...state, ...someNewStuff };
  }
}

And (of course it's obvious to me now) I get an undefined state before the first action run, not the initialState. So I had to add the

default: return state;

to correctly process the '@@init' action type. AFAIK that's nowhere to be found in the docs currently, guess it's just implied, but maybe mentioning it somewhere could save someone else a bit of time.

@burmisov burmisov changed the title Reducers (ex-'stores') must have a 'default' return value for state to be initialized properly? Reducers (ex-'stores') must have a default return value for state to be initialized properly? Jun 23, 2015
@gaearon
Copy link
Contributor

gaearon commented Jun 23, 2015

Sorry for this.

I'd accept a PR checking that the return value of every store function inside composeStores is not undefined. This should cover the most common use case. Folks not using composeStores are assumed to know what they're doing anyway.

This will also need a test in composeStores.spec.js.

@gaearon
Copy link
Contributor

gaearon commented Jun 23, 2015

(And they're going to be composeReducers in the next version... But we're not there yet ;-)

@gaearon
Copy link
Contributor

gaearon commented Jun 23, 2015

Work in progress on fixing this: #174

@gaearon
Copy link
Contributor

gaearon commented Jun 29, 2015

@burmisov Thoughts on #191?

@gaearon
Copy link
Contributor

gaearon commented Jun 29, 2015

Superseded by #191.

@gaearon gaearon closed this as completed Jun 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants