如何解决以下错误“Uncaught TypeError:无法读取未定义的属性(读取“路径名”)”我在正文上写了另外 2 个错误?

发布于 2025-01-10 09:52:21 字数 4972 浏览 1 评论 0原文

我正在使用反应路由器来更改我的网站的内容,但出现以下错误。

我安装了最新版本并尝试遵循文档中给出的新语法,但它仍然无法正常工作。我不断收到所有这些模棱两可的错误。有谁知道如何修复它?

Uncaught TypeError: Cannot read properties of undefined (reading 'pathname')
    at Router (index.tsx:280:1)
    at renderWithHooks (react-dom.development.js:14985:1)
    at mountIndeterminateComponent (react-dom.development.js:17811:1)
    at beginWork (react-dom.development.js:19049:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
    at invokeGuardedCallback (react-dom.development.js:4056:1)
    at beginWork$1 (react-dom.development.js:23964:1)
    at performUnitOfWork (react-dom.development.js:22776:1)
    at workLoopSync (react-dom.development.js:22707:1)

The above error occurred in the <Router> component:
at Router (http://localhost:3000/static/js/bundle.js:37497:15)
at App (http://localhost:3000/static/js/bundle.js:42:74)

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.

我的代码----

app.js

    import React, { useState } from "react";
    import "./App.css";
    import Navbar from "./component/Navbar";
    import TextForm from "./component/TextForm";
    import Alert from "./component/Alert";
    import About from "./component/About";
    import { Routes, Route, Router, Link } from 'react-router-dom';
    function App() {
    return (
        <>
        <Router>
        <Navbar
              title="TextUtils"
              aboutText="About TextUtils"
              mode={mode}
              toggleMode={toggleMode}
            />
            <Alert alert={alert} />
            <div className="container my-3">
              <Routes>
              <Route path="/about" element={<About />}>
                </Route>
                <Route path="/" element={<TextForm heading="Enter text to analyze"  mode={mode} showAlert={showAlert} />}>
                </Route>
              </Routes>
        </div>
        </Router>
        </>
      );
    }
    
    export default App;

navbar.js

    import React from "react";
    import PropTypes from "prop-types";
    import { Link } from "react-router-dom";
    export default function Navbar(props) {
      return (
        <div>
          <nav
            className={`navbar navbar-expand-lg navbar-${props.mode} bg-${props.mode}`}
          >
            <div className="container-fluid">
              <a className="navbar-brand" href="/">
                {props.title}
              </a>
              <button
                className="navbar-toggler"
                type="button"
                data-bs-toggle="collapse"
                data-bs-target="#navbarSupportedContent"
                aria-controls="navbarSupportedContent"
                aria-expanded="false"
                aria-label="Toggle navigation"
              >
                <span className="navbar-toggler-icon"></span>
              </button>
              <div className="collapse navbar-collapse" id="navbarSupportedContent">
                <ul className="navbar-nav me-auto mb-2 mb-lg-0">
                  <li className="nav-item">
                    <Link className="nav-link active" aria-current="page" to="/">
                      Home
                    </Link>
                  </li>
                  <li className="nav-item">
                    <Link className="nav-link" to="/about">
                      {props.aboutText}
                    </Link>
                  </li>
                </ul>
                <div
                  className={`form-check form-switch text-${
                    props.mode === "light" ? "dark" : "light"
                  }`}
                >
                  <input
                    className="form-check-input"
                    onClick={props.toggleMode}
                    type="checkbox"
                    role="switch"
                    id="flexSwitchCheckDefault"
                  />
                  <label
                    className="form-check-label"
                    htmlFor="flexSwitchCheckDefault"
                  >
                    Enable Dark mode
                  </label>
                </div>
              </div>
            </div>
          </nav>
        </div>
      );
    }
    
    Navbar.propTypes = {
      title: PropTypes.string.isRequired,
      aboutText: PropTypes.string.isRequired,
    };
    
    Navbar.defaultProp = {
      title: "Set title here",
      aboutText: "set about text here",
    };`

I am using react-router to change the content of my website and I am getting the following errors.

I installed the latest version and tried to follow the new syntax given on the document but it's still not working. I keep getting all these ambiguous errors. Does anyone know how to fix it?

Uncaught TypeError: Cannot read properties of undefined (reading 'pathname')
    at Router (index.tsx:280:1)
    at renderWithHooks (react-dom.development.js:14985:1)
    at mountIndeterminateComponent (react-dom.development.js:17811:1)
    at beginWork (react-dom.development.js:19049:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
    at invokeGuardedCallback (react-dom.development.js:4056:1)
    at beginWork$1 (react-dom.development.js:23964:1)
    at performUnitOfWork (react-dom.development.js:22776:1)
    at workLoopSync (react-dom.development.js:22707:1)

The above error occurred in the <Router> component:
at Router (http://localhost:3000/static/js/bundle.js:37497:15)
at App (http://localhost:3000/static/js/bundle.js:42:74)

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://reactjs.org/link/error-boundaries to learn more about error boundaries.

My Code----

app.js

    import React, { useState } from "react";
    import "./App.css";
    import Navbar from "./component/Navbar";
    import TextForm from "./component/TextForm";
    import Alert from "./component/Alert";
    import About from "./component/About";
    import { Routes, Route, Router, Link } from 'react-router-dom';
    function App() {
    return (
        <>
        <Router>
        <Navbar
              title="TextUtils"
              aboutText="About TextUtils"
              mode={mode}
              toggleMode={toggleMode}
            />
            <Alert alert={alert} />
            <div className="container my-3">
              <Routes>
              <Route path="/about" element={<About />}>
                </Route>
                <Route path="/" element={<TextForm heading="Enter text to analyze"  mode={mode} showAlert={showAlert} />}>
                </Route>
              </Routes>
        </div>
        </Router>
        </>
      );
    }
    
    export default App;

navbar.js

    import React from "react";
    import PropTypes from "prop-types";
    import { Link } from "react-router-dom";
    export default function Navbar(props) {
      return (
        <div>
          <nav
            className={`navbar navbar-expand-lg navbar-${props.mode} bg-${props.mode}`}
          >
            <div className="container-fluid">
              <a className="navbar-brand" href="/">
                {props.title}
              </a>
              <button
                className="navbar-toggler"
                type="button"
                data-bs-toggle="collapse"
                data-bs-target="#navbarSupportedContent"
                aria-controls="navbarSupportedContent"
                aria-expanded="false"
                aria-label="Toggle navigation"
              >
                <span className="navbar-toggler-icon"></span>
              </button>
              <div className="collapse navbar-collapse" id="navbarSupportedContent">
                <ul className="navbar-nav me-auto mb-2 mb-lg-0">
                  <li className="nav-item">
                    <Link className="nav-link active" aria-current="page" to="/">
                      Home
                    </Link>
                  </li>
                  <li className="nav-item">
                    <Link className="nav-link" to="/about">
                      {props.aboutText}
                    </Link>
                  </li>
                </ul>
                <div
                  className={`form-check form-switch text-${
                    props.mode === "light" ? "dark" : "light"
                  }`}
                >
                  <input
                    className="form-check-input"
                    onClick={props.toggleMode}
                    type="checkbox"
                    role="switch"
                    id="flexSwitchCheckDefault"
                  />
                  <label
                    className="form-check-label"
                    htmlFor="flexSwitchCheckDefault"
                  >
                    Enable Dark mode
                  </label>
                </div>
              </div>
            </div>
          </nav>
        </div>
      );
    }
    
    Navbar.propTypes = {
      title: PropTypes.string.isRequired,
      aboutText: PropTypes.string.isRequired,
    };
    
    Navbar.defaultProp = {
      title: "Set title here",
      aboutText: "set about text here",
    };`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

百思不得你姐 2025-01-17 09:52:21

您能点击下面的 github 链接吗?因为我已经处理了路由器配置,并且您还将了解如何访问最新版本中的参数。

点击此处的 gitHub 链接

Could you please follow the below github link? in that I have handled the router configuration and also you will get some idea on how to access the params in the latest version.

Click gitHub link here

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