Javascript 反应路由器 v^4.0.0 未捕获的类型错误:无法读取未定义的属性“位置”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42892488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
react router v^4.0.0 Uncaught TypeError: Cannot read property 'location' of undefined
提问by Lucas fersa
I've been having some trouble with react router (i'm using version^4.0.0).
我在使用 react 路由器时遇到了一些麻烦(我使用的是 ^4.0.0 版)。
this is my index.js
这是我的 index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
import { Router, Route, Link, browserHistory } from 'react-router';
ReactDOM.render(
<Router history={browserHistory} >
<Route path="/" component={App} />
</Router>,
document.getElementById('root')
);
the App.js is just anything. i'm posting the basic one here, cause it's not the problem (i believe)
App.js 就是任何东西。我在这里发布基本的,因为这不是问题(我相信)
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
and this is what happens when i check the console log
这就是我检查控制台日志时发生的情况
Router.js:43 Uncaught TypeError: Cannot read property 'location' of undefined
at new Router (Router.js:43)
at ReactCompositeComponent.js:295
at measureLifeCyclePerf (ReactCompositeComponent.js:75)
at ReactCompositeComponentWrapper._constructComponentWithoutOwner (ReactCompositeComponent.js:294)
at ReactCompositeComponentWrapper._constructComponent (ReactCompositeComponent.js:280)
at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:188)
at Object.mountComponent (ReactReconciler.js:46)
at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:371)
at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:258)
at Object.mountComponent (ReactReconciler.js:46)
oh, and this is the package.json just in case
哦,这是 package.json 以防万一
{
"name": "teste2",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-router": "^4.0.0"
},
"devDependencies": {
"react-scripts": "0.9.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
i've been checking this on other places, but i didn't find a way to solve it.
我一直在其他地方检查这个,但我没有找到解决它的方法。
Thank you guys very much for your patience and help!!
非常感谢你们的耐心和帮助!!
回答by Tyler McGinnis
You're doing a few things wrong.
你做错了几件事。
First, browserHistory isn't a thing in V4, so you can remove that.
Second, you're importing everything from
react-router, it should bereact-router-dom.Third,
react-router-domdoesn't export aRouter, instead, it exports aBrowserRouterso you need toimport { BrowserRouter as Router } from 'react-router-dom.
首先,browserHistory 在 V4 中不是一个东西,所以你可以删除它。
其次,您要从 导入所有内容
react-router,它应该是react-router-dom.第三,
react-router-dom不导出 aRouter,而是导出 aBrowserRouter所以您需要import { BrowserRouter as Router } from 'react-router-dom.
Looks like you just took your V3 app and expected it to work with v4, which isn't a great idea.
看起来您刚刚使用了 V3 应用程序并希望它能够与 v4 一起使用,这不是一个好主意。
回答by Lancelot
I've tried everything suggested here but didn't work for me. So in case I can help anyone with a similar issue, every single tutorial I've checked is not updated to work with version 4.
我已经尝试了这里建议的所有内容,但对我不起作用。因此,如果我可以帮助遇到类似问题的任何人,我检查过的每个教程都不会更新为适用于版本 4。
Here is what I've done to make it work
这是我为使其工作所做的工作
import React from 'react';
import App from './App';
import ReactDOM from 'react-dom';
import {
HashRouter,
Route
} from 'react-router-dom';
ReactDOM.render((
<HashRouter>
<div>
<Route path="/" render={()=><App items={temasArray}/>}/>
</div>
</HashRouter >
), document.getElementById('root'));
That's the only way I have managed to make it work without any errors or warnings.
这是我设法使其正常工作而没有任何错误或警告的唯一方法。
In case you want to pass props to your component for me the easiest way is this one:
如果您想为我将道具传递给您的组件,最简单的方法是:
<Route path="/" render={()=><App items={temasArray}/>}/>
回答by Ajay Poriya
Replace
代替
import { Router, Route, Link, browserHistory } from 'react-router';
With
和
import { BrowserRouter as Router, Route } from 'react-router-dom';
It will start working. It is because react-router-dom exports BrowserRouter
它将开始工作。这是因为 react-router-dom 导出 BrowserRouter

