Last active
April 6, 2018 01:36
-
-
Save tracker1/d379b92399427e293f13f2a325e85e27 to your computer and use it in GitHub Desktop.
redux-thunk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {createAction} from 'redux-actions' | |
import { | |
SOMETHING_STARTED, | |
SOMETHING_SUCCESS, | |
SOMETHING_ERROR | |
} from './constants'; | |
export const somethingStarted = createAction(SOMETHING_STARTED); | |
export const somethingSucceeded = createAction(SOMETHING_SUCCESS); | |
export const somethingFailed= createAction(SOMETHING_ERROR); | |
export const doSomethingAsync = (input) => async (dispatch,getState) => { | |
try { | |
dispatch(somethingStarted()); | |
var data = await axios.post(...); //or fetch, whatever returns a promise | |
var state = getState(); | |
//manipulate action to dispatch based on state and result of async call | |
var payload = {data,state}; | |
dispatch(somethingSucceeded(payload)); | |
} catch(err) { | |
dispatch(somethingFailed(err)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment