Re:Show HN: Build your first real world React.js application
by hamandcheese on Wednesday September 07, 2016 @04:57PM
(#12447603)
Is it acceptable to dispatch inside your action creators? I'm somewhat new to redux but I got the impression that dispatch should be used inside mapDispatchToProps.
[ Parent
| on HN ]
Re:Show HN: Build your first real world React.js application
by yawgmoth on Wednesday September 07, 2016 @05:45PM
(#12448067)
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.
Re:Show HN: Build your first real world React.js application
by hamandcheese on Thursday September 08, 2016 @01:56PM
(#12455385)
Yes, but in the parent comment a thunk wasn't being dispatched, but rather a closure around some unknown dispatch function is being called.
Re:Show HN: Build your first real world React.js application
by sixo on Wednesday September 07, 2016 @08:31PM
(#12449255)
I found that redux only made sense once I started using redux-thunk to dispatch and handle quite a bit of logic inside the action-creators.
Re:Show HN: Build your first real world React.js application
by hamandcheese on Thursday September 08, 2016 @01:57PM
(#12455408)
My question was because if you look closely it isn't actually a thunk.