Skip to content

Commit 47b89f6

Browse files
kengorabtimdorr
authored andcommitted
Updating docs for default values w/ preloadedState (reduxjs#2341)
Fixes reduxjs#2338, including a note about default values in reducers, even when using preloadedState
1 parent e58e207 commit 47b89f6

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

docs/recipes/reducers/InitializingState.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Initializing State
22

3-
There are two main ways to initialize state for your application. The `createStore` method can accept an optional `preloadedState` value as its second argument. Reducers can also specify an initial value by looking for an incoming state argument that is `undefined`, and returning the value they'd like to use as a default. This can either be done with an explicit check inside the reducer, or by using the ES6 default argument value syntax: `function myReducer(state = someDefaultValue, action)`.
3+
There are two main ways to initialize state for your application. The `createStore` method can accept an optional `preloadedState` value as its second argument. Reducers can also specify an initial value by looking for an incoming state argument that is `undefined`, and returning the value they'd like to use as a default. This can either be done with an explicit check inside the reducer, or by using the ES6 default argument value syntax: `function myReducer(state = someDefaultValue, action)`.
44

55
It's not always immediately clear how these two approaches interact. Fortunately, the process does follow some predictable rules. Here's how the pieces fit together.
66

@@ -13,6 +13,8 @@ With `combineReducers()` the behavior is more nuanced. Those reducers whose stat
1313

1414
**In general, `preloadedState` wins over the state specified by the reducer. This lets reducers specify initial data that makes sense *to them* as default arguments, but also allows loading existing data (fully or partially) when you're hydrating the store from some persistent storage or the server.**
1515

16+
*Note: Reducers whose initial state is populated using `preloadedState` will **still need to provide a default value** to handle when passed a `state` of `undefined`. All reducers are passed `undefined` on initialization, so they should be written such that when given `undefined`, some value should be returned. This can be any non-`undefined` value; there's no need to duplicate the section of `preloadedState` here as the default.*
17+
1618

1719
## In Depth
1820

0 commit comments

Comments
 (0)