Skip to content

Commit 6805327

Browse files
committed
Update store.md
1 parent d64f1b4 commit 6805327

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

docs/store.md

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ The store has the following responsibilities:
99

1010
####Initialization
1111

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.
1313

1414
```js
15-
import { createStore } from 'redux';
15+
import { createStore, combineReducers } from 'redux';
1616
import { Provider } from 'react-redux';
1717
import counter from 'reducers/counter';
1818
import todos from 'reducers/todos';
1919

20-
const store = createStore({counter, todos});
20+
const reducer = combineReducers({counter, todos});
21+
const store = createStore(reducer);
2122
```
2223

23-
A recommended pattern is to create the object of reducer functions from a definition file.
24+
A recommended pattern is to create the object passed to `combineReducers` from a definition file.
2425

2526
```js
2627
// reducers/index.js
@@ -29,29 +30,29 @@ export { default as todos } from './todos'
2930
```
3031

3132
```js
32-
import { createStore } from 'redux';
33-
import { Provider } from 'react-redux';
33+
import { createStore, combineReducers } from 'redux';
3434
import * as reducers from './reducers/index';
3535

36+
const reducer = combineReducer(reducers);
3637
const store = createStore(reducers);
3738
```
3839

3940
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.
4041

4142
```js
4243
// server
43-
const store = createStore(reducers);
44+
const store = createStore(reducer);
4445
store.dispatch(MyActionCreators.doSomething()); // fire action creators to fill the state
4546
const state = store.getState(); // somehow pass this state to the client
4647

4748
// client
4849
const initialState = window.STATE_FROM_SERVER;
49-
const store = createStore(reducers, initialState);
50+
const store = createStore(reducer, initialState);
5051
```
5152

5253
####Usage
5354

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.
5556

5657
```js
5758
store.getState();
@@ -65,7 +66,7 @@ store.getState();
6566
// }
6667
```
6768

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.
6970

7071
```js
7172
store.dispatch({
@@ -95,23 +96,15 @@ let unsubscribe = store.subscribe(() => console.log('state change!'));
9596
```
9697

9798
####Advanced Intitialization
98-
`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?
99-
100-
```js
101-
import { createStore, combineReducers } from 'redux';
102-
import * as reducers from './reducers/index';
103-
104-
const combinedReducerFn = combineReducers(counter, todos);
105-
const store = createStore(combinedReducerFn);
106-
```
10799

108100
[Middleware](middleware.md) can be set up using `applyMiddleware`.
109101

110102
```js
111-
import { createStore, applyMiddleware } from 'redux'
103+
import { createStore, applyMiddleware, combineReducers } from 'redux'
112104
import { logger, promise } from './middleware'
113105
import * as reducers from './reducers/index';
114106

115-
const createWithMiddleware = applyMiddleware(logger, promise)(createStore);
116-
const store = createWithMiddleware(reducers);
107+
const reducer = combineReducers(reducers);
108+
const createStoreWithMiddleware = applyMiddleware(logger, promise)(createStore);
109+
const store = createStoreWithMiddleware(reducer);
117110
```

0 commit comments

Comments
 (0)