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/api/applyMiddleware.md
+66-23Lines changed: 66 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,11 @@ function logger({ getState }) {
37
37
}
38
38
}
39
39
40
-
let store =createStore(todos, ['Use Redux'], applyMiddleware(logger))
40
+
let store =createStore(
41
+
todos,
42
+
['Use Redux'],
43
+
applyMiddleware(logger)
44
+
)
41
45
42
46
store.dispatch({
43
47
type:'ADD_TODO',
@@ -73,6 +77,7 @@ function makeASandwich(forPerson, secretSauce) {
73
77
secretSauce
74
78
}
75
79
}
80
+
76
81
functionapologize(fromPerson, toPerson, error) {
77
82
return {
78
83
type:'APOLOGIZE',
@@ -81,35 +86,60 @@ function apologize(fromPerson, toPerson, error) {
81
86
error
82
87
}
83
88
}
89
+
84
90
functionwithdrawMoney(amount) {
85
91
return {
86
92
type:'WITHDRAW',
87
93
amount
88
94
}
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.
91
106
functionmakeASandwichWithSecretSauce(forPerson) {
107
+
92
108
// Invert control!
93
109
// Return a function that accepts `dispatch` so we can dispatch later.
94
110
// Thunk middleware knows how to turn thunk async actions into actions.
} // 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
}) // 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.
106
132
functionmakeSandwichesForEverybody() {
107
-
returnfunction(dispatch, getState) {
133
+
returnfunction(dispatch, getState) {
108
134
if (!getState().sandwiches.isShopOpen) {
135
+
109
136
// You don't have to return Promises, but it's a handy convention
110
137
// so the caller can always call .then() on async dispatch result.
111
138
returnPromise.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.
@@ -126,26 +156,39 @@ function makeSandwichesForEverybody() {
126
156
)
127
157
)
128
158
}
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
+
130
164
import { renderToString } from'react-dom/server'
165
+
131
166
store
132
167
.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.
0 commit comments