[英]'react-router' does not contain an export named 'browserHistory'
So I'm trying to use history
in React, but it seems to be moved around a lot. 因此,我试图在React中使用
history
,但它似乎在很多地方都在变化。 I found this: 我找到了这个:
https://github.com/ReactTraining/react-router/blob/ab4552d2ea0ec5c0cf3c534bca654a1af3ea0dec/docs/guides/NavigatingOutsideOfComponents.md https://github.com/ReactTraining/react-router/blob/ab4552d2ea0ec5c0cf3c534bca654a1af3ea0dec/docs/guides/NavigatingOutsideOfComponents.md
but it just says that 'react-router' does not contain an export named 'browserHistory'.
但是它只是说
'react-router' does not contain an export named 'browserHistory'.
How can I solve it? 我该如何解决? Where can I find history?
在哪里可以找到历史记录? All answers I found were around 1.5 years old.
我发现的所有答案都在1.5岁左右。
You're looking for the BrowserRouter
. 您正在寻找
BrowserRouter
。
import { BrowserRouter } from 'react-router';
browserHistory
is something that existed in v2/v3 but was rewritten into BrowserRouter
in v4. browserHistory
是v2 / v3中存在的内容,但已在v4中重写为BrowserRouter
。 There is a migration guide here that should help you. 这里有一个迁移指南可以为您提供帮助。
history Api
has been moved to a separate history package history Api
已移至单独的历史包
In order to create browserHistory
, you install history like 为了创建
browserHistory
,您需要安装历史记录,例如
npm install -S history
and write 和写
import createHistory from "history/createBrowserHistory"
const browserHistory = createHistory()
export { browserHistory };
However in react-router-v4
an equivalent of Router
with browserHistory
is BrowserRouter
但是在
react-router-v4
,具有browserHistory
的Router
等效于BrowserRouter
You no longer have browserHistory
apparently. 您显然不再具有
browserHistory
。 So you can do this instead, 所以你可以这样做
<Route>
<Header />
</Route>
and you can do 你可以做
function Header(props) {
// props.history and props.location are available now inside this component
}
and then you can do props.history.push
or whatever if that is what you want to do. 然后您就可以执行
props.history.push
或任何您想做的事情。
The link you have is for the older version of react-router
so check out react-router 4 您拥有的链接适用于较旧版本的
react-router
因此请查看react-router 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.