Skip to content

Commit a9ce9a2

Browse files
committed
Replaced the word 'reddit' with 'subreddit' where subreddit is the correct term.
1 parent 7489e57 commit a9ce9a2

File tree

2 files changed

+114
-116
lines changed

2 files changed

+114
-116
lines changed

docs/advanced/AsyncActions.md

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -45,60 +45,60 @@ We’ll use separate types in this tutorial.
4545

4646
## Synchronous Action Creators
4747

48-
Let’s start by defining the several synchronous action types and action creators we need in our example app. Here, the user can select a reddit to display:
48+
Let’s start by defining the several synchronous action types and action creators we need in our example app. Here, the user can select a subreddit to display:
4949

5050
#### `actions.js`
5151

5252
```js
53-
export const SELECT_REDDIT = 'SELECT_REDDIT'
53+
export const SELECT_SUBREDDIT = 'SELECT_SUBREDDIT'
5454

55-
export function selectReddit(reddit) {
55+
export function selectSubreddit(subreddit) {
5656
return {
57-
type: SELECT_REDDIT,
58-
reddit
57+
type: SELECT_SUBREDDIT,
58+
subreddit
5959
}
6060
}
6161
```
6262

6363
They can also press a “refresh” button to update it:
6464

6565
```js
66-
export const INVALIDATE_REDDIT = 'INVALIDATE_REDDIT'
66+
export const INVALIDATE_SUBREDDIT = 'INVALIDATE_SUBREDDIT'
6767

68-
export function invalidateReddit(reddit) {
68+
export function invalidateSubreddit(subreddit) {
6969
return {
70-
type: INVALIDATE_REDDIT,
71-
reddit
70+
type: INVALIDATE_SUBREDDIT,
71+
subreddit
7272
}
7373
}
7474
```
7575

7676
These were the actions governed by the user interaction. We will also have another kind of action, governed by the network requests. We will see how to dispatch them later, but for now, we just want to define them.
7777

78-
When it’s time to fetch the posts for some reddit, we will dispatch a `REQUEST_POSTS` action:
78+
When it’s time to fetch the posts for some subreddit, we will dispatch a `REQUEST_POSTS` action:
7979

8080
```js
8181
export const REQUEST_POSTS = 'REQUEST_POSTS'
8282

83-
export function requestPosts(reddit) {
83+
export function requestPosts(subreddit) {
8484
return {
8585
type: REQUEST_POSTS,
86-
reddit
86+
subreddit
8787
}
8888
}
8989
```
9090

91-
It is important for it to be separate from `SELECT_REDDIT` or `INVALIDATE_REDDIT`. While they may occur one after another, as the app grows more complex, you might want to fetch some data independently of the user action (for example, to prefetch the most popular reddits, or to refresh stale data once in a while). You may also want to fetch in response to a route change, so it’s not wise to couple fetching to some particular UI event early on.
91+
It is important for it to be separate from `SELECT_SUBREDDIT` or `INVALIDATE_SUBREDDIT`. While they may occur one after another, as the app grows more complex, you might want to fetch some data independently of the user action (for example, to prefetch the most popular subreddits, or to refresh stale data once in a while). You may also want to fetch in response to a route change, so it’s not wise to couple fetching to some particular UI event early on.
9292

9393
Finally, when the network request comes through, we will dispatch `RECEIVE_POSTS`:
9494

9595
```js
9696
export const RECEIVE_POSTS = 'RECEIVE_POSTS'
9797

98-
export function receivePosts(reddit, json) {
98+
export function receivePosts(subreddit, json) {
9999
return {
100100
type: RECEIVE_POSTS,
101-
reddit,
101+
subreddit,
102102
posts: json.data.children.map(child => child.data),
103103
receivedAt: Date.now()
104104
}
@@ -109,7 +109,7 @@ This is all we need to know for now. The particular mechanism to dispatch these
109109

110110
>##### Note on Error Handling
111111
112-
>In a real app, you’d also want to dispatch an action on request failure. We won’t implement error handling in this tutorial, but the [real world example](../introduction/Examples.md#real-world) shows one of the possible approaches.
112+
>In a real app, you’d also want to dispatch an action on request failure. We won’t implement error handling in this tutorial, but the [real world example](../introduction/Examples.html#real-world) shows one of the possible approaches.
113113
114114
## Designing the State Shape
115115

@@ -123,8 +123,8 @@ Here’s what the state shape for our “Reddit headlines” app might look like
123123

124124
```js
125125
{
126-
selectedReddit: 'frontend',
127-
postsByReddit: {
126+
selectedSubreddit: 'frontend',
127+
postsBySubreddit: {
128128
frontend: {
129129
isFetching: true,
130130
didInvalidate: false,
@@ -138,7 +138,7 @@ Here’s what the state shape for our “Reddit headlines” app might look like
138138
{
139139
id: 42,
140140
title: 'Confusion about Flux and Relay'
141-
},
141+
},
142142
{
143143
id: 500,
144144
title: 'Creating a Simple Application Using React JS and Flux Architecture'
@@ -159,11 +159,11 @@ There are a few important bits here:
159159
160160
>In this example, we store the received items together with the pagination information. However, this approach won’t work well if you have nested entities referencing each other, or if you let the user edit items. Imagine the user wants to edit a fetched post, but this post is duplicated in several places in the state tree. This would be really painful to implement.
161161
162-
>If you have nested entities, or if you let users edit received entities, you should keep them separately in the state as if it was a database. In pagination information, you would only refer to them by their IDs. This lets you always keep them up to date. The [real world example](../introduction/Examples.md#real-world) shows this approach, together with [normalizr](https://github.com/gaearon/normalizr) to normalize the nested API responses. With this approach, your state might look like this:
162+
>If you have nested entities, or if you let users edit received entities, you should keep them separately in the state as if it was a database. In pagination information, you would only refer to them by their IDs. This lets you always keep them up to date. The [real world example](../introduction/Examples.html#real-world) shows this approach, together with [normalizr](https://github.com/gaearon/normalizr) to normalize the nested API responses. With this approach, your state might look like this:
163163
164164
>```js
165165
> {
166-
> selectedReddit: 'frontend',
166+
> selectedSubreddit: 'frontend',
167167
> entities: {
168168
> users: {
169169
> 2: {
@@ -184,7 +184,7 @@ There are a few important bits here:
184184
> }
185185
> }
186186
> },
187-
> postsByReddit: {
187+
> postsBySubreddit: {
188188
> frontend: {
189189
> isFetching: true,
190190
> didInvalidate: false,
@@ -215,14 +215,14 @@ Before going into the details of dispatching actions together with network reque
215215
```js
216216
import { combineReducers } from 'redux'
217217
import {
218-
SELECT_REDDIT, INVALIDATE_REDDIT,
218+
SELECT_SUBREDDIT, INVALIDATE_SUBREDDIT,
219219
REQUEST_POSTS, RECEIVE_POSTS
220220
} from '../actions'
221221
222-
function selectedReddit(state = 'reactjs', action) {
222+
function selectedSubreddit(state = 'reactjs', action) {
223223
switch (action.type) {
224-
case SELECT_REDDIT:
225-
return action.reddit
224+
case SELECT_SUBREDDIT:
225+
return action.subreddit
226226
default:
227227
return state
228228
}
@@ -234,7 +234,7 @@ function posts(state = {
234234
items: []
235235
}, action) {
236236
switch (action.type) {
237-
case INVALIDATE_REDDIT:
237+
case INVALIDATE_SUBREDDIT:
238238
return Object.assign({}, state, {
239239
didInvalidate: true
240240
})
@@ -255,44 +255,44 @@ function posts(state = {
255255
}
256256
}
257257
258-
function postsByReddit(state = {}, action) {
258+
function postsBySubreddit(state = {}, action) {
259259
switch (action.type) {
260-
case INVALIDATE_REDDIT:
260+
case INVALIDATE_SUBREDDIT:
261261
case RECEIVE_POSTS:
262262
case REQUEST_POSTS:
263263
return Object.assign({}, state, {
264-
[action.reddit]: posts(state[action.reddit], action)
264+
[action.subreddit]: posts(state[action.subreddit], action)
265265
})
266266
default:
267267
return state
268268
}
269269
}
270270
271271
const rootReducer = combineReducers({
272-
postsByReddit,
273-
selectedReddit
272+
postsBySubreddit,
273+
selectedSubreddit
274274
})
275275
276276
export default rootReducer
277277
```
278278
279279
In this code, there are two interesting parts:
280280

281-
* We use ES6 computed property syntax so we can update `state[action.reddit]` with `Object.assign()` in a terse way. This:
281+
* We use ES6 computed property syntax so we can update `state[action.subreddit]` with `Object.assign()` in a terse way. This:
282282

283283
```js
284284
return Object.assign({}, state, {
285-
[action.reddit]: posts(state[action.reddit], action)
285+
[action.subreddit]: posts(state[action.subreddit], action)
286286
})
287287
```
288288
is equivalent to this:
289289

290290
```js
291291
let nextState = {}
292-
nextState[action.reddit] = posts(state[action.reddit], action)
292+
nextState[action.subreddit] = posts(state[action.subreddit], action)
293293
return Object.assign({}, state, nextState)
294294
```
295-
* We extracted `posts(state, action)` that manages the state of a specific post list. This is just [reducer composition](../basics/Reducers.md#splitting-reducers)! It is our choice how to split the reducer into smaller reducers, and in this case, we’re delegating updating items inside an object to a `posts` reducer. The [real world example](../introduction/Examples.md#real-world) goes even further, showing how to create a reducer factory for parameterized pagination reducers.
295+
* We extracted `posts(state, action)` that manages the state of a specific post list. This is just [reducer composition](../basics/Reducers.md#splitting-reducers)! It is our choice how to split the reducer into smaller reducers, and in this case, we’re delegating updating items inside an object to a `posts` reducer. The [real world example](../introduction/Examples.html#real-world) goes even further, showing how to create a reducer factory for parameterized pagination reducers.
296296

297297
Remember that reducers are just functions, so you can use functional composition and higher-order functions as much as you feel comfortable.
298298

@@ -310,18 +310,18 @@ We can still define these special thunk action creators inside our `actions.js`
310310
import fetch from 'isomorphic-fetch'
311311

312312
export const REQUEST_POSTS = 'REQUEST_POSTS'
313-
function requestPosts(reddit) {
313+
function requestPosts(subreddit) {
314314
return {
315315
type: REQUEST_POSTS,
316-
reddit
316+
subreddit
317317
}
318318
}
319319

320320
export const RECEIVE_POSTS = 'RECEIVE_POSTS'
321-
function receivePosts(reddit, json) {
321+
function receivePosts(subreddit, json) {
322322
return {
323323
type: RECEIVE_POSTS,
324-
reddit,
324+
subreddit,
325325
posts: json.data.children.map(child => child.data),
326326
receivedAt: Date.now()
327327
}
@@ -331,33 +331,33 @@ function receivePosts(reddit, json) {
331331
// Though its insides are different, you would use it just like any other action creator:
332332
// store.dispatch(fetchPosts('reactjs'))
333333

334-
export function fetchPosts(reddit) {
334+
export function fetchPosts(subreddit) {
335335

336336
// Thunk middleware knows how to handle functions.
337337
// It passes the dispatch method as an argument to the function,
338338
// thus making it able to dispatch actions itself.
339339

340-
return dispatch => {
340+
return function (dispatch) {
341341

342342
// First dispatch: the app state is updated to inform
343343
// that the API call is starting.
344344

345-
dispatch(requestPosts(reddit))
345+
dispatch(requestPosts(subreddit))
346346

347347
// The function called by the thunk middleware can return a value,
348348
// that is passed on as the return value of the dispatch method.
349349

350350
// In this case, we return a promise to wait for.
351351
// This is not required by thunk middleware, but it is convenient for us.
352352

353-
return fetch(`https://www.reddit.com/r/${reddit}.json`)
353+
return fetch(`http://www.reddit.com/r/${subreddit}.json`)
354354
.then(response => response.json())
355355
.then(json =>
356356

357357
// We can dispatch many times!
358358
// Here, we update the app state with the results of the API call.
359359

360-
dispatch(receivePosts(reddit, json))
360+
dispatch(receivePosts(subreddit, json))
361361
)
362362

363363
// In a real world app, you also want to
@@ -392,7 +392,7 @@ How do we include the Redux Thunk middleware in the dispatch mechanism? We use t
392392
import thunkMiddleware from 'redux-thunk'
393393
import createLogger from 'redux-logger'
394394
import { createStore, applyMiddleware } from 'redux'
395-
import { selectReddit, fetchPosts } from './actions'
395+
import { selectSubreddit, fetchPosts } from './actions'
396396
import rootReducer from './reducers'
397397
398398
const loggerMiddleware = createLogger()
@@ -404,7 +404,7 @@ const createStoreWithMiddleware = applyMiddleware(
404404
405405
const store = createStoreWithMiddleware(rootReducer)
406406
407-
store.dispatch(selectReddit('reactjs'))
407+
store.dispatch(selectSubreddit('reactjs'))
408408
store.dispatch(fetchPosts('reactjs')).then(() =>
409409
console.log(store.getState())
410410
)
@@ -418,34 +418,34 @@ The nice thing about thunks is that they can dispatch results of each other:
418418
import fetch from 'isomorphic-fetch'
419419

420420
export const REQUEST_POSTS = 'REQUEST_POSTS'
421-
function requestPosts(reddit) {
421+
function requestPosts(subreddit) {
422422
return {
423423
type: REQUEST_POSTS,
424-
reddit
424+
subreddit
425425
}
426426
}
427427

428428
export const RECEIVE_POSTS = 'RECEIVE_POSTS'
429-
function receivePosts(reddit, json) {
429+
function receivePosts(subreddit, json) {
430430
return {
431431
type: RECEIVE_POSTS,
432-
reddit,
432+
subreddit,
433433
posts: json.data.children.map(child => child.data),
434434
receivedAt: Date.now()
435435
}
436436
}
437437

438-
function fetchPosts(reddit) {
438+
function fetchPosts(subreddit) {
439439
return dispatch => {
440-
dispatch(requestPosts(reddit))
441-
return fetch(`https://www.reddit.com/r/${reddit}.json`)
440+
dispatch(requestPosts(subreddit))
441+
return fetch(`http://www.reddit.com/r/${subreddit}.json`)
442442
.then(response => response.json())
443-
.then(json => dispatch(receivePosts(reddit, json)))
443+
.then(json => dispatch(receivePosts(subreddit, json)))
444444
}
445445
}
446446

447-
function shouldFetchPosts(state, reddit) {
448-
const posts = state.postsByReddit[reddit]
447+
function shouldFetchPosts(state, subreddit) {
448+
const posts = state.postsBySubreddit[subreddit]
449449
if (!posts) {
450450
return true
451451
} else if (posts.isFetching) {
@@ -455,7 +455,7 @@ function shouldFetchPosts(state, reddit) {
455455
}
456456
}
457457

458-
export function fetchPostsIfNeeded(reddit) {
458+
export function fetchPostsIfNeeded(subreddit) {
459459

460460
// Note that the function also receives getState()
461461
// which lets you choose what to dispatch next.
@@ -464,9 +464,9 @@ export function fetchPostsIfNeeded(reddit) {
464464
// a cached value is already available.
465465

466466
return (dispatch, getState) => {
467-
if (shouldFetchPosts(getState(), reddit)) {
467+
if (shouldFetchPosts(getState(), subreddit)) {
468468
// Dispatch a thunk from thunk!
469-
return dispatch(fetchPosts(reddit))
469+
return dispatch(fetchPosts(subreddit))
470470
} else {
471471
// Let the calling code know there's nothing to wait for.
472472
return Promise.resolve()
@@ -487,9 +487,9 @@ store.dispatch(fetchPostsIfNeeded('reactjs')).then(() =>
487487

488488
>##### Note about Server Rendering
489489
490-
>Async action creators are especially convenient for server rendering. You can create a store, dispatch a single async action creator that dispatches other async action creators to fetch data for a whole section of your app, and only render after the Promise it returns completes. Then your store will already be hydrated with the state you need before rendering.
490+
>Async action creators are especially convenient for server rendering. You can create a store, dispatch a single async action creator that dispatches other async action creators to fetch data for a whole section of your app, and only render after the Promise it returns, completes. Then your store will already be hydrated with the state you need before rendering.
491491
492-
[Thunk middleware](https://github.com/gaearon/redux-thunk) isn’t the only way to orchestrate asynchronous actions in Redux. You can use [redux-promise](https://github.com/acdlite/redux-promise) or [redux-promise-middleware](https://github.com/pburtchaell/redux-promise-middleware) to dispatch Promises instead of functions. You can dispatch Observables with [redux-rx](https://github.com/acdlite/redux-rx). You can even write a custom middleware to describe calls to your API, like the [real world example](../introduction/Examples.md#real-world) does. It is up to you to try a few options, choose a convention you like, and follow it, whether with, or without the middleware.
492+
[Thunk middleware](https://github.com/gaearon/redux-thunk) isn’t the only way to orchestrate asynchronous actions in Redux. You can use [redux-promise](https://github.com/acdlite/redux-promise) or [redux-promise-middleware](https://github.com/pburtchaell/redux-promise-middleware) to dispatch Promises instead of functions. You can dispatch Observables with [redux-rx](https://github.com/acdlite/redux-rx). You can even write a custom middleware to describe calls to your API, like the [real world example](../introduction/Examples.html#real-world) does. It is up to you to try a few options, choose a convention you like, and follow it, whether with, or without the middleware.
493493

494494
## Connecting to UI
495495

0 commit comments

Comments
 (0)