-
-
Save andrevinsky/e7b45121153420109d3241ec6af5aa70 to your computer and use it in GitHub Desktop.
injection of redux's dispatch in react-router's onEnter hook
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
/* | |
* common react, redux staff here | |
*/ | |
import {Router, createRoutes} from 'react-router'; | |
import createBrowserHistory from 'history/lib/createBrowserHistory'; | |
import rawRoutes from './routes'; | |
import store from './store'; | |
function mixStoreToRoutes(routes) { | |
return routes && routes.map(route => ({ | |
...route, | |
childRoutes: mixStoreToRoutes(route.childRoutes), | |
onEnter: route.onEnter && function (props, replaceState, cb) { | |
route.onEnter(store.dispatch, props) | |
.then(() => cb(null)) | |
.catch(cb); | |
} | |
})); | |
} | |
const routes = mixStoreToRoutes(createRoutes(rawRoutes)); | |
ReactDOM.render( | |
<Provider store={store}> | |
<Router | |
history={createBrowserHistory()} | |
routes={routes} | |
/> | |
</Provider>, | |
document.querySelector('[data-app]') | |
); |
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
import {App, Home, Settings, Signup, Login} from './containers'; | |
import {loadSettings} from './actions'; | |
export default ( | |
<Route> | |
<Route component={App} onEnter={dispatch => dispatch(loadSettings())}> | |
<Route path="/" component={Home}/> | |
<Route path="/settings" component={Settings}/> | |
</Route> | |
<Route path="/signup" component={Signup} /> | |
<Route path="/login" component={Login} /> | |
</Route> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment