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
+36-22Lines changed: 36 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -250,7 +250,7 @@ function posts(state = {
250
250
}
251
251
}
252
252
253
-
functionpostsByReddit(state= {}, action) {
253
+
functionpostsByReddit(state= {}, action) {
254
254
switch (action.type) {
255
255
caseINVALIDATE_REDDIT:
256
256
caseRECEIVE_POSTS:
@@ -293,14 +293,9 @@ Remember that reducers are just functions, so you can use functional composition
293
293
294
294
## Async Action Creators
295
295
296
-
Finally, how do we use the synchronous action creators we [defined earlier](#synchronous-action-creators) together with network requests? The standard
297
-
way to do it with Redux is to use the [redux-thunk middleware](https://github.com/gaearon/redux-thunk). We'll explain how middleware works in general [later](Middleware.md); for now, there
298
-
is just one important thing you need to know: by using this specific middleware, an action creator can return a function instead an action object.
299
-
This way, the function creator becomes a [thunk](https://en.wikipedia.org/wiki/Thunk).
296
+
Finally, how do we use the synchronous action creators we [defined earlier](#synchronous-action-creators) together with network requests? The standard way to do it with Redux is to use the [Redux Thunk middleware](https://github.com/gaearon/redux-thunk). It comes in a separate package called `redux-thunk`. We’ll explain how middleware works in general [later](Middleware.md); for now, there is just one important thing you need to know: by using this specific middleware, an action creator can return a function instead an action object. This way, the function creator becomes a [thunk](https://en.wikipedia.org/wiki/Thunk).
300
297
301
-
Whenafunction creator returns a function, that function will get executed by the Redux Thunk middleware. This function doesn't need to be pure;
302
-
it is thus allowed to have side effects, including executing asynchronous API calls. The function can also dispatch actions -
303
-
like those synchronous actions we defined earlier.
298
+
When a function creator returns a function, that function will get executed by the Redux Thunk middleware. This function doesn’t need to be pure; it is thus allowed to have side effects, including executing asynchronous API calls. The function can also dispatch actions—like those synchronous actions we defined earlier.
304
299
305
300
We can still define these special thunk action creators inside our `actions.js` file:
306
301
@@ -327,28 +322,41 @@ function receivePosts(reddit, json) {
327
322
};
328
323
}
329
324
330
-
// thunk action creator
325
+
// Meet our first thunk action creator!
326
+
// Though its insides are different, you would use it just like any other action creator:
327
+
// store.dispatch(fetchPosts('reactjs'));
328
+
331
329
exportfunctionfetchPosts(reddit) {
332
330
333
-
// thunk middleware knows how to handle functions; it will also pass the dispatch method as an
334
-
// argument to the function, that will thus be able to dispatch actions itself
331
+
// Thunk middleware knows how to handle functions.
332
+
// It passes the dispatch method as an argument to the function,
333
+
// thus making it able to dispatch actions itself.
334
+
335
335
returnfunction (dispatch) {
336
-
337
-
// first dispatch: the app state is updated to inform that the API call is starting
336
+
337
+
// First dispatch: the app state is updated to inform
338
+
// that the API call is starting.
339
+
338
340
dispatch(requestPosts(reddit));
339
341
340
-
// the function called by the thunk middleware can return a value, that is passed on as the return
341
-
// value of the dispatch method which is applied to the thunk action creator.
342
-
343
-
// In this case, we return a promise to wait for;
344
-
// this is not required by thunk middleware, but it is convenient for us
342
+
// The function called by the thunk middleware can return a value,
343
+
// that is passed on as the return value of the dispatch method.
344
+
345
+
// In this case, we return a promise to wait for.
346
+
// This is not required by thunk middleware, but it is convenient for us.
// We can dispatch many times! Here, we update the app state with the results of the API call
351
+
352
+
// We can dispatch many times!
353
+
// Here, we update the app state with the results of the API call.
354
+
349
355
dispatch(receivePosts(reddit, json))
350
356
);
351
-
// NB: in a real world app, you also want to catch any error in the network call
357
+
358
+
// Note: in a real world app, you also want to
359
+
// catch any error in the network call.
352
360
};
353
361
}
354
362
```
@@ -371,8 +379,7 @@ export function fetchPosts(reddit) {
371
379
>import'babel-core/polyfill';
372
380
>```
373
381
374
-
How do we include the Redux-thunk middleware in the dispatch mechanism? We use the applyMiddleware method
375
-
from Redux, as shown below:
382
+
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:
376
383
377
384
#### `index.js`
378
385
@@ -442,6 +449,13 @@ function shouldFetchPosts(state, reddit) {
442
449
}
443
450
444
451
exportfunctionfetchPostsIfNeeded(reddit) {
452
+
453
+
// Note that the function also receives getState()
454
+
// which lets you choose what to dispatch next.
455
+
456
+
// This is useful for avoiding a network request if
Copy file name to clipboardExpand all lines: docs/advanced/AsyncFlow.md
+2-3Lines changed: 2 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,8 @@ Asynchronous middleware like [redux-thunk](https://github.com/gaearon/redux-thun
8
8
9
9
When the last middleware in the chain dispatches an action, it has to be a plain object. This is when the [synchronous Redux data flow](../basics/DataFlow.md) takes place.
10
10
11
-
Check out [the full source code for the async example](ExampleRedditAPI.md)
11
+
Check out [the full source code for the async example](ExampleRedditAPI.md).
12
12
13
13
## Next Steps
14
14
15
-
Now you saw an example of what middleware can do in Redux, it's time to learn how it actually works, and how you can create your own.
16
-
Go on to the next detailed section about [Middleware](/docs/advanced/Middleware.md).
15
+
Now you saw an example of what middleware can do in Redux, it’s time to learn how it actually works, and how you can create your own. Go on to the next detailed section about [Middleware](Middleware.md).
Copy file name to clipboardExpand all lines: docs/advanced/Middleware.md
+1-2Lines changed: 1 addition & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,6 @@
1
1
# Middleware
2
2
3
-
You've seen middleware in action in the [Async Actions](/docs/advanced/AsyncActions.md) example. If you've used server-side libraries like [Express](http://expressjs.com/) and [Koa](http://koajs.com/),
4
-
you were also probably already familiar with the concept of *middleware*. In these frameworks, middleware is some code you can put between the framework receiving a request, and framework generating a response. For example, Express or Koa middleware may add CORS headers, logging, compression, and more. The best feature of middleware is that it’s composable in a chain. You can use multiple independent third-party middleware in a single project.
3
+
You’ve seen middleware in action in the [Async Actions](/docs/advanced/AsyncActions.md) example. If you’ve used server-side libraries like [Express](http://expressjs.com/) and [Koa](http://koajs.com/), you were also probably already familiar with the concept of *middleware*. In these frameworks, middleware is some code you can put between the framework receiving a request, and framework generating a response. For example, Express or Koa middleware may add CORS headers, logging, compression, and more. The best feature of middleware is that it’s composable in a chain. You can use multiple independent third-party middleware in a single project.
5
4
6
5
Redux middleware solves different problems than Express or Koa middleware, but in a conceptually similar way. **It provides a third-party extension point between dispatching an action, and the moment it reaches the reducer.** People use Redux middleware for logging, crash reporting, talking to an asynchronous API, routing, and more.
0 commit comments