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
Decorate your top-level component with `@provider(dispatcher)` (or `<Provider dispatcher={dispatcher}>` inside) to bind it to a Redux dispatcher instance.
196
-
197
-
Redux dispatcher accepts a single Store as an argument. Usually Flux apps have many Stores, so Redux provides a `composeStore` method that turns an object with Store functions as values (such as what you'd get from `import * as stores`) into a Store that [composes](https://gist.github.com/gaearon/d77ca812015c0356654f) them.
198
-
199
-
Think of `composeStores` as a “higher-order” Store because it creates a Store from several Stores. (You don't have to use it! You can just pass your own top-level Store function if that's what you prefer.)
195
+
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:
Then pass `redux` as a prop to `<Provider>` component in the root component of your app, and you're all set:
209
205
206
+
```js
210
207
exportdefaultclassApp {
211
208
render() {
212
209
return (
213
-
<Provider dispatcher={dispatcher}>
210
+
<Provider redux={redux}>
214
211
{() =>
215
-
/* Yep, function as a child. */
216
-
<div>
217
-
<CounterApp />
218
-
<TodoApp />
219
-
</div>
212
+
<CounterApp />
220
213
}
221
214
</Provider>
222
215
);
223
216
}
224
217
}
225
218
```
226
219
220
+
#### Running the same code on client and server
221
+
222
+
The `redux` instance returned by `createRedux` also has the `dispatch(action)`, `subscribe()` and `getState()` methods that you may call outside the React components.
223
+
224
+
You may optionally specify the initial state as the second argument to `createRedux`. This is useful for hydrating the state you received from running Redux on the server:
225
+
226
+
```js
227
+
// server
228
+
constredux=createRedux(stores);
229
+
redux.dispatch(MyActionCreators.doSomething()); // fire action creators to fill the state
230
+
conststate=redux.getState(); // somehow pass this state to the client
231
+
232
+
// client
233
+
constinitialState=window.STATE_FROM_SERVER;
234
+
constredux=createRedux(stores, initialState);
235
+
```
236
+
237
+
#### Additional customization
238
+
239
+
There is also a longer way to do the same thing, if you need additional customization.
// Compose all your Stores into a single Store function with `composeStores`:
257
+
conststore=composeStores(stores);
258
+
259
+
// Create a Dispatcher function for your composite Store:
260
+
constdispatcher=createDispatcher(store);
261
+
262
+
// Create a Redux instance using the dispatcher function:
263
+
constredux=createRedux(dispatcher);
264
+
```
265
+
266
+
Why would you want to write it longer? Maybe you're an advanced user and want to provide a custom Dispatcher function, or maybe you have a different idea of how to compose your Stores (or you're satisfied with a single Store). Redux lets you do all of this.
0 commit comments