React路由器:无法读取属性' pathname'不确定的
我刚刚开始学习反应,并陷入了这个错误。
未定义 在新路由器
这是我的代码:
var React = require('react');
var ReactDOM = require('react-dom');
var { Route, Router, IndexRoute } = require('react-router');
var hashHistory = require('react-router-redux')
var Main = require('./components/Main');
ReactDOM.render(
<Router history={hashHistory}>
<Route path="/" component={Main}>
</Route>
</Router>,
document.getElementById('app')
);
我遵循的教程使用了react-router 2.0.0,但是在桌面上,我使用的是4.1.1。我尝试搜索更改,但在找到有效的解决方案方面没有成功。
"dependencies": {
"express": "^4.15.2",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1",
"react-router-redux": "^4.0.8"
I've just started learning React and got stuck at this error.
Uncaught TypeError: Cannot read property 'pathname' of undefined
at new Router
Here is my code:
var React = require('react');
var ReactDOM = require('react-dom');
var { Route, Router, IndexRoute } = require('react-router');
var hashHistory = require('react-router-redux')
var Main = require('./components/Main');
ReactDOM.render(
<Router history={hashHistory}>
<Route path="/" component={Main}>
</Route>
</Router>,
document.getElementById('app')
);
The tutorial I was following uses React-Router 2.0.0, but on my desktop I'm using 4.1.1. I tried searching for changes but was unsuccessful in finding a solution that worked.
"dependencies": {
"express": "^4.15.2",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1",
"react-router-redux": "^4.0.8"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
这可能会帮助您进行React-Router-Dom v6
This might helps you in react-router-dom v6
在某些情况下,您需要像以下编辑那样定义主路由器上的位置道具
:必须使用“历史记录”的CreateBrowserhistory()定义历史记录
in some cases you need to define the location props on the main Router like this
Edit: history has to be defined with createBrowserHistory() from 'history'
转到index.js file和Wrap
&lt; app/&gt;
内部&lt; browerRoute&gt;
您完成了:0
Go to index.js file and wrap
<App/>
inside<BrowerRoute>
and you are done :0
它无法正常工作,给我这个类型错误:TypeError:无法读取未定义的属性(读取'PathName')
不幸的是,当我使用NavLink时,我正在输入错误的属性。
你可以解决这个问题
尝试以正确的方式记住写“到”属性。
it was not working and giving me this type error: TypeError: Cannot read properties of undefined (reading 'pathname')
unfortunately, i was typing mistake the to attribute when i was using NavLink.
you can solve this
try to remember write "to" attribute in the correct way.
我收到了上述错误消息,这非常神秘。我尝试了提到的其他解决方案,但它不起作用。
事实证明,我不小心忘记将
to
prop包括在&lt; link/&gt;
组件之一中。即使
&lt; link/&gt;
不需要to
prop,您只需添加和写入空引号 - &gt;例如&lt;链接到=“” /&gt; < /code>。那会解决这个问题!
希望错误消息更清晰。一个简单的
所需的prop“未找到”
或类似的东西会有所帮助。希望这样可以节省某些时间遇到同样问题的人。I got the above error message and it was extremely cryptic. I tried the other solutions mentioned and it didn't work.
Turns out I accidentally forgot to include the
to
prop in one of the<Link />
components.Even if the
<Link />
does not require ato
prop, you simply just add and write it with empty quotes -> e.g.<Link to="" />
. That will do the trick!Wish the error message was clearer. A simple
required prop "to" not found
or something similar would have been helpful. Hopefully, this saves someone else who has encountered the same problem some time.错误是因为React路由器V4中的API完全不同。
您可以尝试一下:
您可以查看文档现在。
在这里我有一个带有路由动画的回购。
和在这里您可以找到一个实时演示。
The error is because the api in React Router v4 is totally different.
You can try this:
You can review the documentation to learn how it works now.
Here I have a repo with routing animation.
And here you can find a live demo.
事实证明,进口物和路由器的顺序应该首先
不正确
正确
It turns out that the order of import matter and Router should come first
Incorrect
Correct
只需确保您已将
添加到
props props&lt; link&gt;
否则您也会遇到相同的错误。添加后,它应该看起来像这样: -
Just make sure that you've added
to
props to<Link>
otherwise you would also get the same error.after adding it should look something like this:-
我有相同的错误,我通过更新
历史
从2.xx
到4.xx
解决了它。I had the same error and I resolved it by updating
history
from2.x.x
to4.x.x
只需使用浏览器作为路由器,而不是路由器
Just use BrowserRouter as Router, instead of Router
我有不同的原因,但是同样的错误,因此遇到了这个问题。如果其他人也会发生同样的情况,这可能会有所帮助。
对我来说,问题是我已更新为
react-router-4
,并且访问pathName
已更改。更改:
(react-router-3&amp; prort)
this.props.location.pathname
to: (react-router-4)
this.props.history.location.pathname
I had a different cause, but the same error and came across this question because of it. Putting this here in case the same happens for others and it may help.
The issue for me was that I had updated to
react-router-4
and accessingpathname
has changed.Change:
(React-Router-3 & earlier)
this.props.location.pathname
To: (React-Router-4)
this.props.history.location.pathname
使用browserrouter而不是路由器作为包装器,并且不要忘记导入browserrouter
当您使用链接并忘记提及=“/” prop时也会出现此错误。
Use BrowserRouter instead of Router as a wrapper, and don't forget to import BrowserRouter
This error also arises when u are using Link and forget to mention to="/" prop.
React路由器V6中的API已从V5变化。
请参阅下面的接受答案:
” ')“在V6 React路由器中测试页面时
The API in React Router v6 has changed from that of v5.
See the accepted answer in the following:
"Cannot read properties of undefined (reading 'pathname')" when testing pages in the v6 React Router
检查您应用程序中的所有链接或NAVLink组件,其中一些可能会缺少“到”属性,该属性负责将用户重定向到另一个路径
Check all the Link or NavLink components in your application some of them might be missing the 'to' property which responsible to redirect the user to another path
如果您通过
未定义
或null
值 &lt>&lt; link/&gt; 。This error still happen if you pass
undefined
ornull
value to propsto
of<Link />
.您为链接元素的“到”道具给出了不确定的价值
You are giving an undefined value to the "to" props of Link element
在React-Router-dom 6+中,我会遇到此错误
错误:
解决方案:
In react-router-dom 6+ I'm getting this error
Error:
Solution:
您可以纠正导入: -
import {browserrouter作为路由器,路由,路由}从“ react-router-dom”;
>you can Correct import:-
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
当我尝试使用
history.props('./ somelink')
时,出现了类似的错误。我一直发现路径名不确定的错误。事实证明,我还在下面的JSX中使用了&lt;/link&gt;/link&gt;
。这就是导致错误的原因。希望这对任何犯同样错误的人有帮助had a similar error showing up when I was trying to use
history.props('./somelink')
. I kept getting the error that the pathname was undefined. Turns out that I was also was using a<Link></Link>
in my JSX below. This was what was causing the error. Hope this helps for anyone making the same mistake对于React-Router v6,您应该执行此操作(使用元素并将所有路线封闭在: https://stackover.com/a/stackover.com/a/a/a/702506122 /570612
For react-router v6 you should do this (use element and enclose all routes in `: https://stackoverflow.com/a/70250612/570612