Skip to content

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

@burmisov

Description

@burmisov

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions