Created
September 5, 2017 21:10
-
-
Save Calvin-Huang/512f5386e988e23e614204319f20fa97 to your computer and use it in GitHub Desktop.
Use redux-thunk to handle action sequences.
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 { 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