为什么我的链接在React-Router中不起作用?

发布于 2025-02-12 21:11:06 字数 1621 浏览 0 评论 0 原文

试图为网站创建一个关于IM的网站的页面,我在堆栈上找到了此解决方案,但对我不起作用。我正在为原始代码使用过时的教程,这是我当前的代码:

about.js

import React from "react";
import { Link, Route, useMatch } from "react-router-dom";
import SinglePage from "./SinglePage";

const About = () => {
    //const match = useMatch('/');
    return (
    <div className="about__content">
      <ul className="about__list">
        <li>
          <Link to={'about-app'}>About App</Link>
        </li>
        <li>
          <Link to={'about-author'}>About Author</Link>
        </li>
      </ul>
      <Route path={':slug'}>
        <SinglePage />
      </Route>
    </div>
  );
};

export default About;

index.js 我正在呈现组件:

import React from "react";
import ReactDOM from "react-dom";
import TodoContainer from "./functionBased/components/TodoContainer"; // Component file
import "./functionBased/App.css"; // Style sheet
import { HashRouter as Router, Routes, Route } from "react-router-dom"; // Router file
import About from "./functionBased/pages/About";
import NotMatch from "./functionBased/pages/NotMatch";

ReactDOM.render(
  <React.StrictMode>
    <Router>
      <Routes>
        <Route exact path="/" element={<TodoContainer />} />
        <Route path="/about/*" element={<About />} />
        <Route path="*" element={<NotMatch />} />
      </Routes>
    </Router>
  </React.StrictMode>,
  document.getElementById("root")
);

Trying to create an about page for a website im working on, I found this solution on Stack but it does not work for me. I was using an outdated tutorial for my original code, this is my current code:

About.js:

import React from "react";
import { Link, Route, useMatch } from "react-router-dom";
import SinglePage from "./SinglePage";

const About = () => {
    //const match = useMatch('/');
    return (
    <div className="about__content">
      <ul className="about__list">
        <li>
          <Link to={'about-app'}>About App</Link>
        </li>
        <li>
          <Link to={'about-author'}>About Author</Link>
        </li>
      </ul>
      <Route path={':slug'}>
        <SinglePage />
      </Route>
    </div>
  );
};

export default About;

Index.js where I am rendering the component:

import React from "react";
import ReactDOM from "react-dom";
import TodoContainer from "./functionBased/components/TodoContainer"; // Component file
import "./functionBased/App.css"; // Style sheet
import { HashRouter as Router, Routes, Route } from "react-router-dom"; // Router file
import About from "./functionBased/pages/About";
import NotMatch from "./functionBased/pages/NotMatch";

ReactDOM.render(
  <React.StrictMode>
    <Router>
      <Routes>
        <Route exact path="/" element={<TodoContainer />} />
        <Route path="/about/*" element={<About />} />
        <Route path="*" element={<NotMatch />} />
      </Routes>
    </Router>
  </React.StrictMode>,
  document.getElementById("root")
);

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

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

发布评论

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

评论(2

第几種人 2025-02-19 21:11:07

问题

  1. 关于组件的直接渲染路由组件。 路由组件只能由路由组件或其他路由组件作为嵌套路由。
  2. react-router-dom@6 路由组件在 element prop上渲染其内容。

解决方案

  • 导入路由组件,并包装douscentent 路由由`左右呈现的组件。
  • 在路由的元素 prop上渲染单页

示例:

import React from "react";
import { Link, Routes, Route } from "react-router-dom";
import SinglePage from "./SinglePage";

const About = () => {
  return (
    <div className="about__content">
      <ul className="about__list">
        <li>
          <Link to="about-app">About App</Link>
        </li>
        <li>
          <Link to="about-author">About Author</Link>
        </li>
      </ul>
      <Routes>
        <Route path=":slug" element={<SinglePage />} />
      </Routes>
    </div>
  );
};

export default About;

“

替代方案

,您可以将单页移动作为嵌套路由(而不是作为后裔的位置)移动到主路由器路线)。

示例:

import React from "react";
import { Link, Outlet } from "react-router-dom";
import SinglePage from "./SinglePage";

const About = () => {
  return (
    <div className="about__content">
      <ul className="about__list">
        <li>
          <Link to="about-app">About App</Link>
        </li>
        <li>
          <Link to="about-author">About Author</Link>
        </li>
      </ul>
      <Outlet />
    </div>
  );
};

export default About;

...

<Router>
  <Routes>
    <Route path="/" element={<TodoContainer />} />
    <Route path="/about" element={<About />}>
      <Route path=":slug" element={<SinglePage />} />
    </Route>
    <Route path="*" element={<NotMatch />} />
  </Routes>
</Router>

Issues

  1. The About component is directly rendering a Route component. The Route component can only be rendered by a Routes component or another Route component as a nested route.
  2. The react-router-dom@6 Route components render their content on the element prop.

Solution

  • Import the Routes component and wrap the descendent Route component rendered by `About.
  • Render SinglePage on the route's element prop.

Example:

import React from "react";
import { Link, Routes, Route } from "react-router-dom";
import SinglePage from "./SinglePage";

const About = () => {
  return (
    <div className="about__content">
      <ul className="about__list">
        <li>
          <Link to="about-app">About App</Link>
        </li>
        <li>
          <Link to="about-author">About Author</Link>
        </li>
      </ul>
      <Routes>
        <Route path=":slug" element={<SinglePage />} />
      </Routes>
    </div>
  );
};

export default About;

Edit why-do-my-link-to-links-not-work-in-react-router

Alternative

You could alternatively move the SinglePage route out to the main router as a nested route (instead of where it is as a descendent route).

Example:

import React from "react";
import { Link, Outlet } from "react-router-dom";
import SinglePage from "./SinglePage";

const About = () => {
  return (
    <div className="about__content">
      <ul className="about__list">
        <li>
          <Link to="about-app">About App</Link>
        </li>
        <li>
          <Link to="about-author">About Author</Link>
        </li>
      </ul>
      <Outlet />
    </div>
  );
};

export default About;

...

<Router>
  <Routes>
    <Route path="/" element={<TodoContainer />} />
    <Route path="/about" element={<About />}>
      <Route path=":slug" element={<SinglePage />} />
    </Route>
    <Route path="*" element={<NotMatch />} />
  </Routes>
</Router>

Edit why-do-my-link-to-links-not-work-in-react-router (forked)

千年*琉璃梦 2025-02-19 21:11:07

您正在用/about/*定义路由,并使用 of tosemething 访问它们,该路由根本不存在,添加 \ of of \ auter to 链接中。

You are defining the routes with /about/* and accessing them with about-something which does not exist at all, add \about\author in to for Link.

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