How to reset react-router on page reload

using "response-router": "^ 3.0.2",

When I reload the page, I need it to go to the default view that it is currently doing. However, the actual route that is in the story still matches the one from which I made the update. So the component is remounted when it should not be.

routing history

export const browserHistory = useRouterHistory(useBeforeUnload(createHistory))() 

routes

  <Router history={browserHistory}> <Route path='/' name='Auth' component={Auth}> <IndexRoute component={Dashboard} onEnter={(nextState, replace) => replace('/login')} /> <Route path='dashboard' name='Dashboard' component={Dashboard} /> <Route path='resources' name='Resources' component={Resources} /> <Route path='users' name='Users' component={UsersContainer} /> <Route path='user/:params' name='User' component={UserContainer} /> </Route> <Route path='/' name='NoAuth' component={NoAuth}> <Route path='login' name='Login Page' component={Login} /> </Route> </Router> 

This is how I check if the user still has a valid session token and how I am redirected to the control panel. Not sure if I am doing this in the best way.

  const _checkAuth = () => { if (profile) { const res = JSON.parse(profile) store.dispatch({ type: types.LOGIN_IDENTITY_SUCCESS, res }) console.log(browserHistory.getCurrentLocation().pathname) router.replace('/dashboard') } } _checkAuth() 
+5
source share
1 answer

if you want to click on a new route using a repeater, you can use the .push method to push another route to the stack, this will not delete the first route in the history, if that is what you are looking for, but it will correctly push the new route into reaction router

 const _checkAuth = () => { if (profile) { const res = JSON.parse(profile) store.dispatch({ type: types.LOGIN_IDENTITY_SUCCESS, res }) console.log(browserHistory.getCurrentLocation().pathname) browserHistory.push('/dashboard'); } } _checkAuth() 

Just replace router.replace('/dashboard') with browserHistory.push('/dashboard')

+3
source

Source: https://habr.com/ru/post/1264655/


All Articles