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
+30-16Lines changed: 30 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -13,48 +13,62 @@ To intialize a store you simply call `createStore` with a reducer:
13
13
14
14
```js
15
15
import { createStore } from'redux';
16
-
importcounterfrom'./reducers/counter';
16
+
importcounterReducerfrom'./reducers/counter';
17
17
18
-
conststore=createStore(counter);
18
+
conststore=createStore(counterReducer);
19
19
```
20
20
21
-
`createStore` intializes the store with a single reducer, but in the following example we would like to use functionality from both the `counter` and `todos` reducers. To do this we need to somehow combine `counter` and `todos` into a single reducer. Here is one approach:
21
+
`createStore` intializes the store with a single reducer, but in the following example we would like to use functionality from both the `counter` and `todos` reducers. To do this we need to somehow combine `counter` and `todos` into a single reducer. Here is one approach:
// combine updated state created by each reducer into the new combined state
40
40
return { counterState, todoState };
41
41
}
42
42
43
-
conststore=createStore(combinedReducer);
43
+
conststore=createStore(masterReducer);
44
44
```
45
45
46
-
As combining reducers is so common there is a helper function named `combineReducers` to assist. `combineReducers` takes an object of reducers and returns them combined into a single reducer. Here is the previous example using `combineReducers`:
46
+
Combining reducers is very common so there is a helper function named `combineReducers` to assist. `combineReducers` takes an object of reducers and combines them into a single reducer. Here is the previous example using `combineReducers`:
Note that the key of each reducer in the reducer object passed to `combineReducers` becomes a top-level key on the state object returned by the combined reducer. In the previous example, the state object returned by `masterReducer` looks like this:
63
+
64
+
```js
65
+
conststate= {
66
+
counter: counterState,
67
+
todos: todosState
68
+
};
55
69
```
56
70
57
-
A recommended pattern is to import the object passed to `combineReducers` from a definition file:
71
+
A recommended pattern is to use `import *` to import an object of reducers from a definition file:
58
72
59
73
```js
60
74
// reducers/index.js
@@ -85,7 +99,7 @@ const store = createStore(reducer, initialState);
85
99
86
100
####Usage
87
101
88
-
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.
102
+
Store state is accessed using the `getState` method.
0 commit comments