Last active
September 5, 2017 19:53
-
-
Save Calvin-Huang/87534c0df8fb25cd6a3e493bd85e6ecd to your computer and use it in GitHub Desktop.
Handle action sequence in middleware
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
const middleware = (store) => { | |
return next => action => { | |
// Dispathcing action. | |
const returnValue = next(action); | |
// Action dispatched. | |
const state = store.getState(); | |
switch (action.type) { | |
case START_REQUEST: { | |
fetch('/api/v1/foo') | |
.then(response => response.json()) | |
.then((data) => { | |
store.dispatch(receive(data)); | |
}) | |
.catch((error) => { | |
store.dispatch(showNotification(error)); | |
}); | |
break; | |
} | |
} | |
} | |
}; | |
const store = createStore( | |
rootReducer, | |
applyMiddleware(middleware), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment