Skip to content

Commit 4b8418f

Browse files
committed
Merge pull request reduxjs#255 from ellbee/stores-doc
First attempt at updating docs for Redux Store
2 parents 7e5a829 + d64f1b4 commit 4b8418f

File tree

1 file changed

+108
-18
lines changed

1 file changed

+108
-18
lines changed

docs/store.md

Lines changed: 108 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,117 @@
1-
### Initializing Redux
1+
### The Redux Store
22

3-
The simplest way to initialize a Redux instance is to give it an object whose values are your Store functions, and whose keys are their names. You may `import *` from the file with all your Store definitions to obtain such an object:
3+
The store has the following responsibilities:
4+
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
9+
10+
####Initialization
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.
413

514
```js
6-
import { createRedux } from 'redux';
7-
import { Provider } from 'redux/react';
8-
import * as stores from '../stores/index';
15+
import { createStore } from 'redux';
16+
import { Provider } from 'react-redux';
17+
import counter from 'reducers/counter';
18+
import todos from 'reducers/todos';
919

10-
const redux = createRedux(stores);
20+
const store = createStore({counter, todos});
1121
```
1222

13-
Then pass `redux` as a prop to `<Provider>` component in the root component of your app, and you're all set:
23+
A recommended pattern is to create the object of reducer functions from a definition file.
24+
25+
```js
26+
// reducers/index.js
27+
export { default as counter } from './counter'
28+
export { default as todos } from './todos'
29+
```
1430

1531
```js
16-
export default class App {
17-
render() {
18-
return (
19-
<Provider redux={redux}>
20-
{() =>
21-
<CounterApp />
22-
}
23-
</Provider>
24-
);
25-
}
26-
}
32+
import { createStore } from 'redux';
33+
import { Provider } from 'react-redux';
34+
import * as reducers from './reducers/index';
35+
36+
const store = createStore(reducers);
37+
```
38+
39+
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+
```js
42+
// server
43+
const store = createStore(reducers);
44+
store.dispatch(MyActionCreators.doSomething()); // fire action creators to fill the state
45+
const state = store.getState(); // somehow pass this state to the client
46+
47+
// client
48+
const initialState = window.STATE_FROM_SERVER;
49+
const store = createStore(reducers, initialState);
50+
```
51+
52+
####Usage
53+
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+
56+
```js
57+
store.getState();
58+
// {
59+
// counter: 0,
60+
// todos: [{
61+
// text: 'Use Redux',
62+
// marked: false,
63+
// id: 0
64+
// }];
65+
// }
66+
```
67+
68+
Store state is updated by calling the `dispatch` method with an action understood by one or more reducers.
69+
70+
```js
71+
store.dispatch({
72+
type: INCREMENT_COUNTER
73+
});
74+
store.dispatch({
75+
type: MARK_TODO,
76+
id: 0
77+
});
78+
```
79+
```js
80+
store.getState();
81+
// {
82+
// counter: 1,
83+
// todos: [{
84+
// text: 'Use Redux',
85+
// marked: true,
86+
// id: 0
87+
// }];
88+
// }
89+
```
90+
91+
A listener can be registered with the store by passing a callback to the `subscribe` method. The `subscribe` method returns a function that can later be called to unsubscribe the listener.
92+
93+
```js
94+
let unsubscribe = store.subscribe(() => console.log('state change!'));
95+
```
96+
97+
####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+
```
107+
108+
[Middleware](middleware.md) can be set up using `applyMiddleware`.
109+
110+
```js
111+
import { createStore, applyMiddleware } from 'redux'
112+
import { logger, promise } from './middleware'
113+
import * as reducers from './reducers/index';
114+
115+
const createWithMiddleware = applyMiddleware(logger, promise)(createStore);
116+
const store = createWithMiddleware(reducers);
27117
```

0 commit comments

Comments
 (0)