-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Description
I might be missing something, but I don't understand why a reducer should have a default state. Most of the doc have something like:
function reducer(state = initialState, action) {...}
I don't see why the reducer should know anything about the default state. The state is managed by the store, the reducer is a pure function that does its job with whatever you give it as argument and not trying to guess what the state is if there is none. But well, I though that it was ok as long as I wasn't forced to put a default state inside the reducer.
Except that combineReducers
forces me to put a default state. According to this code, an undefined
state will be triggered by Redux to the reducer. Why? My store will pass the default state, so it will never be undefined
in practice. My reducer is simple: if it knows the action, it will do something, if not, it will return the state. If the state is undefined
, it will return undefined
.
Even worse, what if I want to use the same reducer in several different stores with different initial states? Agreed I can always put a fake default state inside the reducer (like 0
) and it will be overridden by the store default states, whatever they are, but it feels just wrong.