Sometimes, we want to fix onEnter not called in React Router.
In this article, we’ll look at how to fix onEnter not called in React Router.
How to fix onEnter not called in React Router?
To fix onEnter not called in React Router, we can set the render
prop instead of the onEnter
prop to a function that renders the component we want.
For instance, we write
<Route
exact
path="/home"
render={() => (isLoggedIn() ? <Redirect to="/front" /> : <Home />)}
/>
to set the render
prop to a function that renders Redirect
is isLoggedIn
returns true
and Home
otherwise.
Conclusion
To fix onEnter not called in React Router, we can set the render
prop instead of the onEnter
prop to a function that renders the component we want.