Created
August 21, 2016 22:49
-
-
Save JoshBarr/922d7a6ded13e97220966f8ab46cead6 to your computer and use it in GitHub Desktop.
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
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