Created
February 7, 2017 12:09
-
-
Save vitorbal/487a5058b661743e67a0ed86e3b3b532 to your computer and use it in GitHub Desktop.
A Saga that allows you to describe route changes using the `meta.redirect` property of your action objects. FSA-compliant.
This file contains 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
// A saga that looks for a route change request in every action. Example usage: | |
// export const selectConversation = ({ id }) => ({ | |
// type: SELECT_CONVERSATION, | |
// payload: { | |
// conversationId: id, | |
// }, | |
// meta: { | |
// redirect: { | |
// url: `inbox/${id}`, // <-- the saga will redirect to this url | |
// replace: true, // <-- if the route should be replaced or pushed on top of the history stack | |
// } | |
// } | |
// }); | |
function* watchForRouteChanges(action) { | |
if (action.meta && action.meta.redirect) { | |
const { url, replace } = action.meta.redirect; | |
yield put(replace ? replaceRoute(url) : pushRoute(url)); // replaceRoute and pushRoute are action creators | |
} | |
} | |
function* routeChangeSaga() { | |
yield takeEvery('*', watchForRouteChanges); | |
} | |
// in your store configuration: | |
import routeChangeSaga from './sagas' | |
const sagaMiddleware = createSagaMiddleware() | |
const store = ... | |
sagaMiddleware.run(routeChangeSaga) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment