Skip to content

Instantly share code, notes, and snippets.

@Calvin-Huang
Created September 5, 2017 21:10
Show Gist options
  • Save Calvin-Huang/512f5386e988e23e614204319f20fa97 to your computer and use it in GitHub Desktop.
Save Calvin-Huang/512f5386e988e23e614204319f20fa97 to your computer and use it in GitHub Desktop.
Use redux-thunk to handle action sequences.
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/index';
// Note: this API requires redux@>=3.1.0
const store = createStore(
rootReducer,
applyMiddleware(thunk)
);
// An asynchronous action.
function startRequest() {
// Do somthing before async action starts.
return dispatch => {
fetch('/api/v1/foo')
.then(response => response.json())
.then((data) => {
dispatch(receive(data));
})
.catch((error) => {
dispatch(showNotification(error));
});
}
}
// A normal action.
function showNotification(error) {
return {
type: ........
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment