javascript - How to change react-router routes? -
i'm trying change routes depending on logged in state:
renderrouter() { if (loggedin) { return ( <router> <route path="/" component={dashboard} /> </router> ); } return ( <router> <route path="/" component={login} /> </router> ); } but when state changes i'm receiving warning: warning: [react-router] cannot change <router routes>; ignored
is possible reinitialize react-router new routes?
i know use onenter ensure user has access page, need have different components in 1 route according logged in state , don't want move such logic inside of components.
first of create 2 router, don't think should this.
try wrapping routes in switch component in 1 router, use render props of "main" route, redirect if condition true, use exact props sure route match default, notice "/dashboard" route above other, switch can match it.
the result should :
<router> <switch> <route path="/dashboard" component={dashboard} /> <route path="/" exact render={() => { if (loggedin) { return <redirect to="/dashboard" />; } return <login />; }} /> </switch> </router> don't forget import components.
hope helped.
Comments
Post a Comment