Router.create 在 react-router v1.0.0 中

Router.create in react-router v1.0.0

react-router v1.0 中 Router.create (react-router v0.13) 的等效项是什么?

var router = Router.create({
  routes: routes,
  location: null // Router.HistoryLocation
});

我想为要在 DOM 之外使用的路由器创建一个参考。

router.transitionTo('app');

上面的代码片段是v0.13中的代码片段。想知道是否有人知道如何在 react-router v1.0 中编写它们。

我浏览了 react-router 的文档,找到了 createRoutes 函数。我不知道如何使用它。请帮我解决这个问题。

您在 v1.0.0 中为此使用了一个 history 对象 - 如果您没有传递 Router 对象,它将为您创建一个。

请参阅 Getting Started for how to create a history object and Navigation 了解如何使用它执行导航。

从 v1.0 开始,React Router 现在比那个更加模块化。导航之类的东西是 "history" 对象的功能,而不是路由器本身的功能,它只处理路由。

要用 React Router v1.0 做同样的事情,你可以在路由器外实例化 history 对象,然后用它做任何你想做的事。在组件外部导航指南中提供了更详细的文档:https://github.com/rackt/react-router/blob/master/docs/guides/advanced/NavigatingOutsideOfComponents.md.

在 v1 中 Router 是一个组件,routes 是 children,history 是 prop:

import { createHistory } from 'history'
import { Router } from 'react-router'

<Router history={createHistory()}>{routes}</Router>

编辑:现在我建议你仍然安装 history 模块,但是从 react-router 导入 'browserHistory',它在它上面提供了一个薄层

在您的顶级组件中,您可以访问 props.history.pushState(state, pathname, query)(进一步向下,您将必须通过它或将 mixin 与 createClass 或其他东西一起使用):

this.props.history.pushState(null, 'contact');