React Router: setRouteWillLeaveHook

Joe Maddalone
InstructorJoe Maddalone

Share this video with your friends

Send Tweet

setRouteWillLeaveHook provides a method for us to intercept a route change before leaving the current route.

Zhenyang Hua
Zhenyang Hua
~ 5 years ago

Why do we set the 'setRouteWillLeaveHook' in the componentWillMount lifecycle?

Joe Maddalone
Joe Maddaloneinstructor
~ 5 years ago

Great question! My personal preference is to make these types of component alterations before the component has mounted if possible. However, you could just as easily do it in componentDidMount.

Arif
Arif
~ 5 years ago

I wanted to clarify that it is setRouteLeaveHook not setRouteWillLeaveHook. Am I right?

Joe Maddalone
Joe Maddaloneinstructor
~ 5 years ago

Yes. Good catch.

b62
b62
~ 5 years ago

What about routes with limited acces (role based)?

Logan May
Logan May
~ 4 years ago

Here's an article on user roles with React Router: https://hackernoon.com/role-based-authorization-in-react-c70bb7641db4