You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/store.md
+15-22Lines changed: 15 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -9,18 +9,19 @@ The store has the following responsibilities:
9
9
10
10
####Initialization
11
11
12
-
The simplest way to initialize the store is to call `createStore` with an object of reducer functions. The following example sets up the store for an application that has both a `counter` reducer and a `todos` reducer.
12
+
The simplest way to initialize the store is to call `createStore` with a reducer function. The following example has both a `counter` reducer and a `todos` reducer, so they need to be combined into a single reducer using the `combineReducers` function.
You may optionally specify the initial state as the second argument to `createStore`. This is useful for hydrating the state of the client to match the state of a Redux application running on the server.
40
41
41
42
```js
42
43
// server
43
-
conststore=createStore(reducers);
44
+
conststore=createStore(reducer);
44
45
store.dispatch(MyActionCreators.doSomething()); // fire action creators to fill the state
45
46
conststate=store.getState(); // somehow pass this state to the client
46
47
47
48
// client
48
49
constinitialState=window.STATE_FROM_SERVER;
49
-
conststore=createStore(reducers, initialState);
50
+
conststore=createStore(reducer, initialState);
50
51
```
51
52
52
53
####Usage
53
54
54
-
Store state is accessed using the `getState` method. Note that when you initialize the store by passing `createStore` an object of reducer functions, the name of each reducer becomes a top-level key on the state object.
55
+
Store state is accessed using the `getState` method. Note that the name of each reducer in the object passed to `combineReducers` becomes a top-level key on the state object.
55
56
56
57
```js
57
58
store.getState();
@@ -65,7 +66,7 @@ store.getState();
65
66
// }
66
67
```
67
68
68
-
Store state is updated by calling the `dispatch` method with an action understood by one or more reducers.
69
+
Store state is updated by calling the `dispatch` method with an action understood by the reducer.
`createStore` can be called with a single reducing function instead of an object of reducing functions. The `combineReducers` function can be used to compose multiple reducers. TODO: Real world use case?
0 commit comments