React-Router Lifecycle mixin - 已弃用?在 v1.0.3 中使用什么来确认导航?

React-Router Lifecycle mixin - depreacted? What to use instead for confirming navigation in v1.0.3?

我正在 运行ning react-router 1.0.3

我的目标是拦截离开页面的用户并显示标准浏览器确认对话框。

在 Github 页面 [1] 上,页面显示 LifeCycle 混合宏已弃用。 (看起来我可以使用它,但如果它被弃用,也许不应该使用)。

从该页面 [2] 可以看出,在 v.2 中可以使用新功能 router.setRouteLeaveHook(看起来不错)。这将为您设置 listenBeforeLeavingRoute(并解除绑定)。

那么:对于版本 1.0.3,我应该使用什么来将钩子放入离开页面转换事件中?看起来不是 LifeCycle 混入。但是 setRouteLeaveHook 还没有。我也许应该手动实现它;像这样:@props.history.listenBeforeLeavingRoute(@props.route, 回调)?如果我这样做,我需要手动删除 componentWillUnmount 中的侦听器吗? (我相信是这样)。

此外 - v 2.0.0 是否已投入生产?如果我只是 运行 npm install 我得到版本 1.0.3

(React-Router 很棒,但问题是文档似乎总是与当前版本不同步。它的制作者似乎没有将文档视为软件不可或缺的一部分;这,在我看来是)。

  1. https://github.com/rackt/react-router/blob/master/docs/API.md
  2. https://github.com/rackt/react-router/blob/master/upgrade-guides/v2.0.0.md#mixins-are-deprecated

我是 React Router 的维护者之一。

我先回答你的问题。获取有关 React Router v1.0.x 信息的最佳方式是 here (it's a 1.0.x branch). And there, you can find bunch of very useful examples, the one you are looking for is the transitions example.

如果你使用的是 ES6 类,那么你不能使用 mixins,所以你需要使用 history,你可以在 [=] 中找到确认导航的示例13=].

感谢您对文档的反馈。我们没有忽视这个话题,我们知道这是一个问题。 Ryan 正在编写教程(请先睹为快 here), and we are in the process of making the docs better