3 ms·
It's an example of the thunk pattern. Thunks are used when you have an async call, e.g. getFoos. Then your action creator would be function requestFoos() {
by yawgmoth 10y ago
It's an example of the thunk pattern. Thunks are used when you have an async call, e.g. getFoos. Then your action creator would be
function requestFoos() {
return { type: REQ_FOOS }
}
function gotFoos(foos) {
return { type: GOT_FOOS, foos }
}
export function getFoos() {
return function(dispatch) {
dispatch(requestFoos());
myFooProvider
.getFoos()
.then(function(foos) { dispatch(gotFoos(foos)); });
}
}
Really useful for writing reducers that want to keep track of when you're fetching data from the server.
- hamandcheese 10y agoYes, but in the parent comment a thunk wasn't being dispatched, but rather a closure around some unknown dispatch function is being called.