Skip to content

Commit afe4b44

Browse files
committed
Improve formatting
1 parent 1647c26 commit afe4b44

File tree

2 files changed

+70
-24
lines changed

2 files changed

+70
-24
lines changed

docs/api/applyMiddleware.md

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ function logger({ getState }) {
3737
}
3838
}
3939

40-
let store = createStore(todos, ['Use Redux'], applyMiddleware(logger))
40+
let store = createStore(
41+
todos,
42+
['Use Redux'],
43+
applyMiddleware(logger)
44+
)
4145

4246
store.dispatch({
4347
type: 'ADD_TODO',
@@ -73,6 +77,7 @@ function makeASandwich(forPerson, secretSauce) {
7377
secretSauce
7478
}
7579
}
80+
7681
function apologize(fromPerson, toPerson, error) {
7782
return {
7883
type: 'APOLOGIZE',
@@ -81,35 +86,60 @@ function apologize(fromPerson, toPerson, error) {
8186
error
8287
}
8388
}
89+
8490
function withdrawMoney(amount) {
8591
return {
8692
type: 'WITHDRAW',
8793
amount
8894
}
89-
} // Even without middleware, you can dispatch an action:
90-
store.dispatch(withdrawMoney(100)) // But what do you do when you need to start an asynchronous action, // such as an API call, or a router transition? // Meet thunks. // A thunk is a function that returns a function. // This is a thunk.
95+
}
96+
97+
// Even without middleware, you can dispatch an action:
98+
store.dispatch(withdrawMoney(100))
99+
100+
// But what do you do when you need to start an asynchronous action,
101+
// such as an API call, or a router transition?
102+
103+
// Meet thunks.
104+
// A thunk is a function that returns a function.
105+
// This is a thunk.
91106
function makeASandwichWithSecretSauce(forPerson) {
107+
92108
// Invert control!
93109
// Return a function that accepts `dispatch` so we can dispatch later.
94110
// Thunk middleware knows how to turn thunk async actions into actions.
95-
return function(dispatch) {
111+
return function (dispatch) {
96112
return fetchSecretSauce().then(
97113
sauce => dispatch(makeASandwich(forPerson, sauce)),
98114
error => dispatch(apologize('The Sandwich Shop', forPerson, error))
99115
)
100116
}
101-
} // Thunk middleware lets me dispatch thunk async actions // as if they were actions!
102-
store.dispatch(makeASandwichWithSecretSauce('Me')) // It even takes care to return the thunk's return value // from the dispatch, so I can chain Promises as long as I return them.
117+
}
118+
119+
// Thunk middleware lets me dispatch thunk async actions
120+
// as if they were actions!
121+
store.dispatch(makeASandwichWithSecretSauce('Me'))
122+
123+
// It even takes care to return the thunk's return value
124+
// from the dispatch, so I can chain Promises as long as I return them.
103125
store.dispatch(makeASandwichWithSecretSauce('My wife')).then(() => {
104126
console.log('Done!')
105-
}) // In fact I can write action creators that dispatch // actions and async actions from other action creators, // and I can build my control flow with Promises.
127+
})
128+
129+
// In fact I can write action creators that dispatch
130+
// actions and async actions from other action creators,
131+
// and I can build my control flow with Promises.
106132
function makeSandwichesForEverybody() {
107-
return function(dispatch, getState) {
133+
return function (dispatch, getState) {
108134
if (!getState().sandwiches.isShopOpen) {
135+
109136
// You don't have to return Promises, but it's a handy convention
110137
// so the caller can always call .then() on async dispatch result.
111138
return Promise.resolve()
112-
} // We can dispatch both plain object actions and other thunks, // which lets us compose the asynchronous actions in a single flow.
139+
}
140+
141+
// We can dispatch both plain object actions and other thunks,
142+
// which lets us compose the asynchronous actions in a single flow.
113143
return dispatch(makeASandwichWithSecretSauce('My Grandma'))
114144
.then(() =>
115145
Promise.all([
@@ -126,26 +156,39 @@ function makeSandwichesForEverybody() {
126156
)
127157
)
128158
}
129-
} // This is very useful for server side rendering, because I can wait // until data is available, then synchronously render the app.
159+
}
160+
161+
// This is very useful for server side rendering, because I can wait
162+
// until data is available, then synchronously render the app.
163+
130164
import { renderToString } from 'react-dom/server'
165+
131166
store
132167
.dispatch(makeSandwichesForEverybody())
133-
.then(() => response.send(renderToString(<MyApp store={store} />))) // I can also dispatch a thunk async action from a component // any time its props change to load the missing data.
168+
.then(() => response.send(renderToString(<MyApp store={store} />)))
169+
170+
// I can also dispatch a thunk async action from a component
171+
// any time its props change to load the missing data.
172+
134173
import { connect } from 'react-redux'
135174
import { Component } from 'react'
175+
136176
class SandwichShop extends Component {
137177
componentDidMount() {
138178
this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson))
139179
}
180+
140181
componentWillReceiveProps(nextProps) {
141182
if (nextProps.forPerson !== this.props.forPerson) {
142183
this.props.dispatch(makeASandwichWithSecretSauce(nextProps.forPerson))
143184
}
144185
}
186+
145187
render() {
146188
return <p>{this.props.sandwiches.join('mustard')}</p>
147189
}
148190
}
191+
149192
export default connect(state => ({
150193
sandwiches: state.sandwiches
151194
}))(SandwichShop)
@@ -160,19 +203,19 @@ export default connect(state => ({
160203
* If you want to conditionally apply a middleware, make sure to only import it when it's needed:
161204

162205
```js
163-
let middleware = [a, b]
164-
if (process.env.NODE_ENV !== 'production') {
165-
let c = require('some-debug-middleware')
166-
let d = require('another-debug-middleware')
167-
middleware = [...middleware, c, d]
168-
}
206+
let middleware = [a, b]
207+
if (process.env.NODE_ENV !== 'production') {
208+
let c = require('some-debug-middleware')
209+
let d = require('another-debug-middleware')
210+
middleware = [...middleware, c, d]
211+
}
169212

170-
const store = createStore(
171-
reducer,
172-
preloadedState,
173-
applyMiddleware(...middleware)
174-
)
175-
```
213+
const store = createStore(
214+
reducer,
215+
preloadedState,
216+
applyMiddleware(...middleware)
217+
)
218+
```
176219

177220
This makes it easier for bundling tools to cut out unneeded modules and reduces the size of your builds.
178221

docs/api/compose.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import reducer from '../reducers/index'
2525

2626
const store = createStore(
2727
reducer,
28-
compose(applyMiddleware(thunk), DevTools.instrument())
28+
compose(
29+
applyMiddleware(thunk),
30+
DevTools.instrument()
31+
)
2932
)
3033
```
3134

0 commit comments

Comments
 (0)