React路由器:无法读取属性' pathname'不确定的

发布于 2025-02-11 03:30:41 字数 805 浏览 2 评论 0原文

我刚刚开始学习反应,并陷入了这个错误。

未定义 在新路由器

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

递刀给你 2025-02-18 03:30:42

这可能会帮助您进行React-Router-Dom v6

import {  BrowserRouter as Router,  Routes,  Route} from "react-router-dom"

This might helps you in react-router-dom v6

import {  BrowserRouter as Router,  Routes,  Route} from "react-router-dom"
猫烠⑼条掵仅有一顆心 2025-02-18 03:30:42

在某些情况下,您需要像以下编辑那样定义主路由器上的位置道具

ReactDOM.render(
<Router location={history.location} history={hashHistory}>
    <Route path="/" component={Main}>

    </Route>
</Router>, document.getElementById('app'));

:必须使用“历史记录”的CreateBrowserhistory()定义历史记录

in some cases you need to define the location props on the main Router like this

ReactDOM.render(
<Router location={history.location} history={hashHistory}>
    <Route path="/" component={Main}>

    </Route>
</Router>, document.getElementById('app'));

Edit: history has to be defined with createBrowserHistory() from 'history'

日暮斜阳 2025-02-18 03:30:42

转到index.js file和Wrap &lt; app/&gt;内部&lt; browerRoute&gt;

<BrowserRouter>
    <App/>                                                   
</BrowserRouter>

您完成了:0

Go to index.js file and wrap <App/> inside <BrowerRoute>

<BrowserRouter>
    <App/>                                                   
</BrowserRouter>

and you are done :0

衣神在巴黎 2025-02-18 03:30:42

它无法正常工作,给我这个类型错误: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.

哑剧 2025-02-18 03:30:41

我收到了上述错误消息,这非常神秘。我尝试了提到的其他解决方案,但它不起作用。

事实证明,我不小心忘记将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 a to 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.

手心的海 2025-02-18 03:30:41

错误是因为React路由器V4中的API完全不同。
您可以尝试一下:

import React from 'react';
import ReactDOM from 'react-dom';
import {
  BrowserRouter as Router,
  Route
} from 'react-router-dom';

const Main = () => <h1>Hello world</h1>;

ReactDOM.render(
  <Router>
    <Route path='/' component={Main} />
  </Router>,
  document.getElementById('app')
);

您可以查看文档现在。

在这里我有一个带有路由动画的回购。

在这里您可以找到一个实时演示。

The error is because the api in React Router v4 is totally different.
You can try this:

import React from 'react';
import ReactDOM from 'react-dom';
import {
  BrowserRouter as Router,
  Route
} from 'react-router-dom';

const Main = () => <h1>Hello world</h1>;

ReactDOM.render(
  <Router>
    <Route path='/' component={Main} />
  </Router>,
  document.getElementById('app')
);

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.

紫竹語嫣☆ 2025-02-18 03:30:41

事实证明,进口物和路由器的顺序应该首先

不正确

import { BrowserRouter as Route, Router, Routes } from "react-router-dom";

正确

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

It turns out that the order of import matter and Router should come first

Incorrect

import { BrowserRouter as Route, Router, Routes } from "react-router-dom";

Correct

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
千鲤 2025-02-18 03:30:41

只需确保您已将添加到 props props &lt; link&gt;否则您也会遇到相同的错误。

添加后,它应该看起来像这样: -

<Link to="#">

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:-

<Link to="#">
但可醉心 2025-02-18 03:30:41

我有相同的错误,我通过更新历史 2.xx4.xx解决了它。

npm安装历史@最新 - 萨夫

I had the same error and I resolved it by updating history from 2.x.x to 4.x.x

npm install history@latest --save

国产ˉ祖宗 2025-02-18 03:30:41

只需使用浏览器作为路由器,而不是路由器

Just use BrowserRouter as Router, instead of Router

南笙 2025-02-18 03:30:41

我有不同的原因,但是同样的错误,因此遇到了这个问题。如果其他人也会发生同样的情况,这可能会有所帮助。
对我来说,问题是我已更新为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 accessing pathname has changed.

Change:
(React-Router-3 & earlier)

this.props.location.pathname

To: (React-Router-4)

this.props.history.location.pathname

情绪 2025-02-18 03:30:41

使用browserrouter而不是路由器作为包装器,并且不要忘记导入browserrouter

<BrowserRouter>
    // Rest of the app.
</BrowserRouter>

当您使用链接并忘记提及=“/” prop时也会出现此错误。

Use BrowserRouter instead of Router as a wrapper, and don't forget to import BrowserRouter

<BrowserRouter>
    // Rest of the app.
</BrowserRouter>

This error also arises when u are using Link and forget to mention to="/" prop.

望喜 2025-02-18 03:30:41

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

简单气质女生网名 2025-02-18 03:30:41

检查您应用程序中的所有链接或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

旧情别恋 2025-02-18 03:30:41

如果您通过未定义null值 &lt>&lt; link/&gt; 。

This error still happen if you pass undefined or null value to props to of <Link />.

单调的奢华 2025-02-18 03:30:41

您为链接元素的“到”道具给出了不确定的价值

import { Link } from "react-router-dom";

const path = undefined;

function Home() {
  return (
        <Link to={path}>Home</Link> 
  );
}

You are giving an undefined value to the "to" props of Link element

import { Link } from "react-router-dom";

const path = undefined;

function Home() {
  return (
        <Link to={path}>Home</Link> 
  );
}
暮光沉寂 2025-02-18 03:30:41

在React-Router-dom 6+中,我会遇到此错误

错误:

   <Routes>
      <Route path="/about">
        <About />
      </Route>
      <Route path="/users">
        <Users />
      </Route>
      <Route path="/">
        <Home />
      </Route>
   </Routes>

解决方案:

<Routes>
   <Route exact path="/" element={<Home />} />
   <Route exact path="/about" element={<About />} />
   <Route exact path="/user" element={<User />} />
</Routes>

In react-router-dom 6+ I'm getting this error

Error:

   <Routes>
      <Route path="/about">
        <About />
      </Route>
      <Route path="/users">
        <Users />
      </Route>
      <Route path="/">
        <Home />
      </Route>
   </Routes>

Solution:

<Routes>
   <Route exact path="/" element={<Home />} />
   <Route exact path="/about" element={<About />} />
   <Route exact path="/user" element={<User />} />
</Routes>
花海 2025-02-18 03:30:41

您可以纠正导入: -

import {browserrouter作为路由器,路由,路由}从“ react-router-dom”;>

you can Correct import:-

import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

若言繁花未落 2025-02-18 03:30:41

当我尝试使用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

萤火眠眠 2025-02-18 03:30:41

对于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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文