React router v4 get user confirmation when leaving page

时间:2017-06-15 09:39:16

标签: reactjs react-router react-router-v4

In older versions I could use setRouteLeaveHook within my component.

For example (SO): Detecting user leaving page

With react router v4 the logic has changed away from injecting the router itself into the components and I only found the following function on router v4:

BrowserRouter. getUserConfirmation

I am a little bit confused, why I should link the confirm behavior with the Router itself and not with a specific component!?

How can I place a confirm window, when leaving my component (linked to my current route), while being in a certain state? This seems to be not supported by the function above.

2 个答案:

答案 0 :(得分:7)

我认为Prompt component是您正在寻找的。只需在要确认导航表单的组件中呈现它,即在this中呈现的相同组件。

答案 1 :(得分:0)

react-router-navigation-prompt还能满足您的需求:它是功能更强大的<Prompt />