Skip to content

Instantly share code, notes, and snippets.

@kdichev
Last active April 8, 2018 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdichev/3d57c8c3f21751d23f10f83b86361beb to your computer and use it in GitHub Desktop.
Save kdichev/3d57c8c3f21751d23f10f83b86361beb to your computer and use it in GitHub Desktop.
const asyncAction = (dataMaybe) => async (dispatch, getState) => {
// u can dispatch all kinds of actions here according to outcome
dispatch({type: "FETCHING_START"}
// if u need to use store data but can be passed as arg from react component when invoking
const store = getState().theReducer
const response = await myFetchFunction()
if (response) {
dispatch({type: "FETCHING_SUCCESS", payload: {response}}
}
if (error) {
dispatch({type: "FETCHING_ERROR"}
}
};
const mapDispatchToProps = dispatch => ({
handleMySexyAction: () => dispatch({type: "DO_SEXY_STUFF" }),
handleMyAsyncAction: (dataMaybe) => dispatch(asyncAction(dataMaybe))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment