react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Error using the "setRouteLeaveHook" method

Open Aiso786 opened this issue 10 years ago • 1 comments

Version

2.0.0

Steps to reproduce

I have a component where i set a leave hook in the "componentDidMount" event:

this.props.router.setRouteLeaveHook(this.props.route, this.routerWillLeave);

My URL looks like this: http://localhost:4010/#/scope/1/65537/2/2015/2016-12-31T00:00:00/0?_k=5ciwxn

If i change something in the URL, the component renders again but i get an error in the "componentDidMount" event. It fails on the line where i set the hook:

this.props.router.setRouteLeaveHook(this.props.route, this.routerWillLeave);

This is the error i get :

[react-router] adding multiple leave hooks for the same route is deprecated; manage multiple confirmations in your own code instead

Aiso786 avatar Sep 29 '16 12:09 Aiso786

EDIT

I've figured out what my problem is by reading the documentation:

You don't need to manually tear down the route leave hook in most cases. We automatically remove all attached route leave hooks after leaving the associated route.

I think that my route does not change so the hook is not unbinded. The solution is to store the function returned when i create the hook and use it on the "componentWillUnmount" event.

setRouteLeaveHook(route, hook)

Registers the given hook function to run before leaving the given route. .... Returns a function that may be used to unbind the listener.

Aiso786 avatar Sep 29 '16 16:09 Aiso786