什么是传递给React Router <router>历史道具?

时间:2017-03-20 21:40:56

标签: javascript reactjs react-router

我正在使用react路由器(版本4.0.0),Router组件有一个名为“history”的必需道具。

我知道在react-router的先前版本中,您可以从包中导入browserHistoryhashHistory并将其作为history道具传递给<Router>,但是在v4中这些都不可用。

项目可用的文档没有提及有关此必需历史记录herehere的内容,它引用了{4}}中不再提供的hashHistory导出封装

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果要使用包含历史记录的组件,则应导入 来自&#39; react-router

的browserHistory或hashHistory
import { Router,browserHistory } from 'react-router';
<Router history={browserHistory} routes={...} />
or
import { Router,hashHistory } from 'react-router';
<Router history={hashHistory} routes={...} />

https://github.com/ReactTraining/react-router/tree/v3/docs

答案 1 :(得分:0)

我导入的{ Router } from 'react-router-dom'确实有一个必需的道具history,但我需要导入没有必需道具历史记录的{ BrowserRouter as Router } from 'react-router-dom'。这为我解决了这个问题。

相关问题