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/advanced/AsyncActions.md
+9-8Lines changed: 9 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -138,7 +138,7 @@ Here’s what the state shape for our “Reddit headlines” app might look like
138
138
{
139
139
id:42,
140
140
title:'Confusion about Flux and Relay'
141
-
},
141
+
},
142
142
{
143
143
id:500,
144
144
title:'Creating a Simple Application Using React JS and Flux Architecture'
@@ -384,7 +384,7 @@ export function fetchPosts(subreddit) {
384
384
>import'babel-core/polyfill'
385
385
>```
386
386
387
-
How do we include the Redux Thunk middleware in the dispatch mechanism? We use the [`applyMiddleware()`](../api/applyMiddleware.md) method from Redux, as shown below:
387
+
How do we include the Redux Thunk middleware in the dispatch mechanism? We use the [`applyMiddleware()`](../api/applyMiddleware.md) store enhancer from Redux, as shown below:
388
388
389
389
#### `index.js`
390
390
@@ -397,12 +397,13 @@ import rootReducer from './reducers'
397
397
398
398
constloggerMiddleware=createLogger()
399
399
400
-
constcreateStoreWithMiddleware=applyMiddleware(
401
-
thunkMiddleware, // lets us dispatch() functions
402
-
loggerMiddleware // neat middleware that logs actions
403
-
)(createStore)
404
-
405
-
conststore=createStoreWithMiddleware(rootReducer)
400
+
conststore=createStore(
401
+
rootReducer,
402
+
applyMiddleware(
403
+
thunkMiddleware, // lets us dispatch() functions
404
+
loggerMiddleware // neat middleware that logs actions
Copy file name to clipboardExpand all lines: docs/advanced/Middleware.md
+21-18Lines changed: 21 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -272,6 +272,8 @@ The implementation of [`applyMiddleware()`](../api/applyMiddleware.md) that ship
272
272
273
273
* To ensure that you may only apply middleware once, it operates on `createStore()` rather than on `store` itself. Instead of `(store, middlewares) => store`, its signature is `(...middlewares) => (createStore) => createStore`.
274
274
275
+
Because it is cumbersome to apply functions to `createStore()` before using it, `createStore()` accepts an optional last argument to specify such functions.
276
+
275
277
### The Final Approach
276
278
277
279
Given this middleware we just wrote:
@@ -305,13 +307,12 @@ Here’s how to apply it to a Redux store:
Copy file name to clipboardExpand all lines: docs/api/applyMiddleware.md
+16-9Lines changed: 16 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ Middleware is not baked into [`createStore`](createStore.md) and is not a fundam
14
14
15
15
#### Returns
16
16
17
-
(*Function*) A store enhancer that applies the given middleware. The store enhancer is a function that needs to be applied to `createStore`. It will return a different `createStore` which has the middleware enabled.
17
+
(*Function*) A store enhancer that applies the given middleware. The store enhancer signature is `createStore => createStore'` but the easiest way to apply it is to pass it to [`createStore()`](./createStore.md) as the last `enhancer` argument.
18
18
19
19
#### Example: Custom Logger Middleware
20
20
@@ -37,8 +37,11 @@ function logger({ getState }) {
37
37
}
38
38
}
39
39
40
-
let createStoreWithMiddleware =applyMiddleware(logger)(createStore)
41
-
let store =createStoreWithMiddleware(todos, [ 'Use Redux' ])
This makes it easier for bundling tools to cut out unneeded modules and reduces the size of your builds.
236
241
237
242
* Ever wondered what `applyMiddleware` itself is? It ought to be an extension mechanism more powerful than the middleware itself. Indeed, `applyMiddleware` is an example of the most powerful Redux extension mechanism called [store enhancers](../Glossary.md#store-enhancer). It is highly unlikely you’ll ever want to write a store enhancer yourself. Another example of a store enhancer is [redux-devtools](https://github.com/gaearon/redux-devtools). Middleware is less powerful than a store enhancer, but it is easier to write.
238
243
239
244
* Middleware sounds much more complicated than it really is. The only way to really understand middleware is to see how the existing middleware works, and try to write your own. The function nesting can be intimidating, but most of the middleware you’ll find are, in fact, 10-liners, and the nesting and composability is what makes the middleware system powerful.
245
+
246
+
* To apply multiple store enhancers, you may use [`compose()`](./compose.md).
Copy file name to clipboardExpand all lines: docs/api/createStore.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,8 @@ There should only be a single store in your app.
9
9
10
10
2.[`initialState`]*(any)*: The initial state. You may optionally specify it to hydrate the state from the server in universal apps, or to restore a previously serialized user session. If you produced `reducer` with [`combineReducers`](combineReducers.md), this must be a plain object with the same shape as the keys passed to it. Otherwise, you are free to pass anything that your `reducer` can understand.
11
11
12
+
3.[`enhancer`]*(Function)*: The store enhancer. You may optionally specify it to enhance the store with third-party capabilities such as middleware, time travel, persistence, etc. The only store enhancer that ships with Redux is [`applyMiddleware()`](./applyMiddleware.md).
13
+
12
14
#### Returns
13
15
14
16
([*`Store`*](Store.md)): An object that holds the complete state of your app. The only way to change its state is by [dispatching actions](Store.md#dispatch). You may also [subscribe](Store.md#subscribe) to the changes to its state to update the UI.
@@ -49,3 +51,5 @@ console.log(store.getState())
49
51
* For universal apps that run on the server, create a store instance with every request so that they are isolated. Dispatch a few data fetching actions to a store instance and wait for them to complete before rendering the app on the server.
50
52
51
53
* When a store is created, Redux dispatches a dummy action to your reducer to populate the store with the initial state. You are not meant to handle the dummy action directly. Just remember that your reducer should return some kind of initial state if the state given to it as the first argument is `undefined`, and you’re all set.
54
+
55
+
* To apply multiple store enhancers, you may use [`compose()`](./compose.md).
0 commit comments