Skip to content

Commit d8ad1ea

Browse files
committed
Style tweaks
1 parent cc5975a commit d8ad1ea

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

docs/advanced/AsyncActions.md

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function posts(state = {
250250
}
251251
}
252252
253-
function postsByReddit(state = { }, action) {
253+
function postsByReddit(state = {}, action) {
254254
switch (action.type) {
255255
case INVALIDATE_REDDIT:
256256
case RECEIVE_POSTS:
@@ -293,14 +293,9 @@ Remember that reducers are just functions, so you can use functional composition
293293

294294
## Async Action Creators
295295

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).
300297

301-
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;
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.
304299

305300
We can still define these special thunk action creators inside our `actions.js` file:
306301

@@ -327,28 +322,41 @@ function receivePosts(reddit, json) {
327322
};
328323
}
329324

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+
331329
export function fetchPosts(reddit) {
332330

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+
335335
return function (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+
338340
dispatch(requestPosts(reddit));
339341

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.
347+
345348
return fetch(`http://www.reddit.com/r/${reddit}.json`)
346349
.then(response => response.json())
347350
.then(json =>
348-
// 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+
349355
dispatch(receivePosts(reddit, json))
350356
);
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.
352360
};
353361
}
354362
```
@@ -371,8 +379,7 @@ export function fetchPosts(reddit) {
371379
>import 'babel-core/polyfill';
372380
>```
373381
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:
376383
377384
#### `index.js`
378385
@@ -442,6 +449,13 @@ function shouldFetchPosts(state, reddit) {
442449
}
443450

444451
export function fetchPostsIfNeeded(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
457+
// a cached value is already available.
458+
445459
return (dispatch, getState) => {
446460
if (shouldFetchPosts(getState(), reddit)) {
447461
// Dispatch a thunk from thunk!

docs/advanced/AsyncFlow.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ Asynchronous middleware like [redux-thunk](https://github.com/gaearon/redux-thun
88

99
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.
1010

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).
1212

1313
## Next Steps
1414

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

docs/advanced/Middleware.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Middleware
22

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

65
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.
76

0 commit comments

Comments
 (0)