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
+68-28Lines changed: 68 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -3,24 +3,72 @@
3
3
The store has the following responsibilities:
4
4
5
5
* Holds application state
6
-
* Allows access to state via the `getState` method
7
-
* Allows state to be updated via the `dispatch` method
8
-
* Registers listeners via the `subscribe` method
6
+
* Allows access to state via `getState`
7
+
* Allows state to be updated via `dispatch`
8
+
* Registers listeners via `subscribe`
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
+
To intialize a store you simply call `createStore` with a reducer:
13
13
14
14
```js
15
15
import { createStore } from'redux';
16
-
import { Provider } from'react-redux';
17
-
importcounterfrom'reducers/counter';
18
-
importtodosfrom'reducers/todos';
16
+
importcounterReducerfrom'./reducers/counter';
19
17
20
-
conststore=createStore({counter, todos});
18
+
conststore=createStore(counterReducer);
21
19
```
22
20
23
-
A recommended pattern is to create the object of reducer functions from a definition file.
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
+
return { counterState, todoState };
41
+
}
42
+
43
+
conststore=createStore(masterReducer);
44
+
```
45
+
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
+
};
69
+
```
70
+
71
+
A recommended pattern is to use `import *` to import an object of reducers from a definition file:
24
72
25
73
```js
26
74
// reducers/index.js
@@ -29,29 +77,29 @@ export { default as todos } from './todos'
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
88
41
89
```js
42
90
// server
43
-
conststore=createStore(reducers);
91
+
conststore=createStore(reducer);
44
92
store.dispatch(MyActionCreators.doSomething()); // fire action creators to fill the state
45
93
conststate=store.getState(); // somehow pass this state to the client
46
94
47
95
// client
48
96
constinitialState=window.STATE_FROM_SERVER;
49
-
conststore=createStore(reducers, initialState);
97
+
conststore=createStore(reducer, initialState);
50
98
```
51
99
52
100
####Usage
53
101
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.
102
+
Store state is accessed using the `getState` method.
55
103
56
104
```js
57
105
store.getState();
@@ -65,7 +113,7 @@ store.getState();
65
113
// }
66
114
```
67
115
68
-
Store state is updated by calling the `dispatch` method with an action understood by one or more reducers.
116
+
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