Skip to content

Instantly share code, notes, and snippets.

@wyze
Created November 10, 2015 04:32
Show Gist options
  • Save wyze/d70fdc94bfe4e3d3f9ef to your computer and use it in GitHub Desktop.
Save wyze/d70fdc94bfe4e3d3f9ef to your computer and use it in GitHub Desktop.
Redux state passed to onEnter of react-router.
import createHistory from 'history/lib/createBrowserHistory';
import createStore from './redux/createStore';
import createRoutes from './routes';
import React from 'react';
import { Provider } from 'react-redux';
import { Router } from 'react-router';
import { render } from 'react-dom';
const history = createHistory();
const store = createStore(history, window.__data);
render((
<Provider store={store}>
<Router history={history}>
{createRoutes(store)}
</Router>
</Provider>
), document.getElementById('root'));
export default store =>
( nextState, replaceState ) => {
if ( !store.getState().auth.user ) {
replaceState({ nextPathname: nextState.location.pathname }, '/');
}
};
import AppContainer from '../containers/AppContainer';
import createRequireAuth from './createRequireAuth';
import DashboardContainer from '../containers/DashboardContainer';
import HomeContainer from '../containers/HomeContainer';
import React from 'react';
import { Redirect, Route } from 'react-router';
export default store => (
<Route component={AppContainer}>
<Route path="/" component={HomeContainer} />
<Route onEnter={createRequireAuth(store)}>
<Route path="dashboard" component={DashboardContainer} />
</Route>
</Route>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment