React Router: setRouteWillLeaveHook

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

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

Joe Maddalone
Joe Maddalone(instructor)
~ 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
Arif
~ 8 years ago

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

Joe Maddalone
Joe Maddalone(instructor)
~ 8 years ago

Yes. Good catch.

b62
b62
~ 8 years ago

What about routes with limited acces (role based)?

Logan May
Logan May
~ 8 years ago

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