【问题标题】:Prevent navigation with React router v6使用 React 路由器 v6 防止导航
【发布时间】:2022-01-04 02:36:19
【问题描述】:

如果用户离开表单页面,我想弹出一个模式,告诉他们如果他们离开,他们的更改将丢失,并且他们可以选择离开或取消。

我看到最近从 beta 到 V6 的更改取消了对 Prompt、useBlocker 和 useHistory 的使用。我已经看到了使用 onBeforeUnload 的答案,但这似乎只使用内置提示。

有没有办法用 useNavigate 钩子做到这一点?

【问题讨论】:

    标签: react-router navigation


    【解决方案1】:

    这是 GitHub 上的一个类似问题 https://github.com/remix-run/react-router/issues/8139

    一个可能的解决方案是使用UNSAFE_NavigationContext,如果它公开了block 选项。见https://github.com/remix-run/react-router/issues/8139#issuecomment-977790637

    我使用 "react-router-dom": "^6.0.2" 对其进行了测试,现在它 [作为一种解决方法] 有效。

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 2021-03-29
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 2022-11-06
      • 2022-07-09
      • 2018-01-07
      • 2021-12-24
      相关资源
      最近更新 更多