Skip to content

Instantly share code, notes, and snippets.

@JoshBarr
Created August 21, 2016 22:49
Show Gist options
  • Save JoshBarr/922d7a6ded13e97220966f8ab46cead6 to your computer and use it in GitHub Desktop.
Save JoshBarr/922d7a6ded13e97220966f8ab46cead6 to your computer and use it in GitHub Desktop.
const mapStoreToRoutes = (routes, store) => {
return routes && routes.map(route => {
return {
...route,
indexRoute: route.indexRoute && {
...route.indexRoute,
onEnter: route.indexRoute.onEnter && function mappedIndexOnEnter(nextState, replaceState, next) {
route.indexRoute.onEnter(store.dispatch, store.getState(), nextState, replaceState, next);
},
},
childRoutes: mapStoreToRoutes(route.childRoutes, store),
onEnter: route.onEnter && function mappedOnEnter(nextState, replaceState, next) {
route.onEnter(store.dispatch, store.getState(), nextState, replaceState, next);
},
};
});
};
// usage:
import { Route, createRoutes } from 'react-router';
import store from './store';
const handlerWithDispatch = (dispatch, state, nextState, replace, callback) => {
dispatch({ type: 'SOME_ACTION', payload: {} });
callback();
};
const routes = (
<Route path="/" component={App} onEnter={handlerWithDispatch} />
);
const routes = mapStoreToRoutes(createRoutes(routes), store);
...
<Router routes={routes />
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment