OnEnter not called in React-Router

OK, I'm tired of trying.
The onEnter method onEnter not work. Any idea why?

 // Authentication "before" filter function requireAuth(nextState, replace){ console.log("called"); // => Is not triggered at all if (!isLoggedIn()) { replace({ pathname: '/front' }) } } // Render the app render( <Provider store={store}> <Router history={history}> <App> <Switch> <Route path="/front" component={Front} /> <Route path="/home" component={Home} onEnter={requireAuth} /> <Route exact path="/" component={Home} onEnter={requireAuth} /> <Route path="*" component={NoMatch} /> </Switch> </App> </Router> </Provider>, document.getElementById("lf-app") 

Edit:

The method is executed when I call onEnter={requireAuth()} , but obviously this is not the goal, and I will not get the necessary parameters either.

+21
reactjs react-router react-router-v4
Mar 13 '17 at 16:23
source share
1 answer

onEnter no longer exists on react-router-4 . You should use <Route render={ ... } /> to get the desired functionality. I believe the Redirect example has your specific scenario. I modified it below to fit yours.

 <Route exact path="/home" render={() => ( isLoggedIn() ? ( <Redirect to="/front"/> ) : ( <Home /> ) )}/> 
+49
Mar 13 '17 at 16:55
source share



All Articles