使用 React Router v6 进行导航的事件侦听器
我想向 React Router v6 中的 navigate
事件添加一个事件侦听器。我还没有找到任何东西。有这样的功能吗?
编辑:处理事件时,我想阻止导航并将用户路由到另一个目的地,这取决于他来自哪里。
I would like to add an event listener to a navigate
event in react router v6. I have not found anything to it. Is there such a feature?
Edit: When the event is handled, I want to prevent the navigation and route the user to another destination, which depends on where he is comming from.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以
useLocation()
和useEffect ()
。You can
useLocation()
anduseEffect()
.一旦位置发生变化,您可能会只使用导航功能,
这对用户来说看起来非常难看,但
如果您只是想阻止导航,react-router v6 中有一个名为
useBlocker
的新钩子,可用于在指定的布尔值为 true 时显示警报。请参阅此处的 React Router Dom - v6 - useBlocker 问题You might get away with just using the navigate function once the location changes
this will look very ugly for the user tho
If you just want to block the navigation there is a new hook in react-router v6 called
useBlocker
, which can be used to display an alert when the specified boolean is true. Refer to React Router Dom - v6 - useBlocker issue here