React Router: setRouteWillLeaveHook

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
~ 8 years ago

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

Joe Maddaloneinstructor
~ 8 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
~ 8 years ago

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

Joe Maddaloneinstructor
~ 8 years ago

Yes. Good catch.

b62
~ 8 years ago

What about routes with limited acces (role based)?

Logan May
~ 8 years ago

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