Skip to content

Instantly share code, notes, and snippets.

@andrevinsky
Forked from wtfil/index.js
Created November 15, 2016 16:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrevinsky/e7b45121153420109d3241ec6af5aa70 to your computer and use it in GitHub Desktop.
Save andrevinsky/e7b45121153420109d3241ec6af5aa70 to your computer and use it in GitHub Desktop.
injection of redux's dispatch in react-router's onEnter hook
/*
* 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]')
);
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