Skip to content

Instantly share code, notes, and snippets.

@Calvin-Huang
Last active September 5, 2017 19:53
Show Gist options
  • Save Calvin-Huang/87534c0df8fb25cd6a3e493bd85e6ecd to your computer and use it in GitHub Desktop.
Save Calvin-Huang/87534c0df8fb25cd6a3e493bd85e6ecd to your computer and use it in GitHub Desktop.
Handle action sequence in middleware
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