Redux-thunk allows you to create action-creators that return a function instead of an action. The inner function receives the store methods dispatch and getState as parameters.
function incrementAsync() {
return (dispatch, getState) => {
setTimeout(() => {
dispatch(increment());
}, 1000);
};
}
But at the same time, react-redux' connect
already has a mapDispatchToProps
argument that can be used to wrap action creator into a dispatch call so they may be invoked directly. With mapDispatchToProps you can already do,
const mapDispatchToProps = (dispatch) => ({
incrementAsync: () => {
setTimeout(() => {
dispatch(increment());
}, 1000);
}
});
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent)
In my eyes, the same can be accomplished without redux-thunk. Why do we have the redux-thunk library in the first place? I'm sure I'm just failing to see it since redux-thunk is a fairly popular library.