不能使用 React 路由元素

huangapple go评论58阅读模式
英文:

Can't use react router element

问题

I am gonna create react router structure in my React JS app. I have an issue. The issue is I can't use react-router element. So I have a component Main Page. I want to display after the page is loaded. Also, I have a nav component I want to display all the time. But I can see only the nav component. Main Page component is found. I don't have any errors, but I can't see it. I can't see its content.

Index js file. Main file in my app:

import React from 'react';
import {
  createBrowserRouter,
  RouterProvider,
} from "react-router-dom";
import MainPage from "./components/MainPage";
import ReactDOM from 'react-dom/client';
import Nav from "./components/Nav";

const router = createBrowserRouter([
  {
    path: "/",
    element: <Nav />,
    children: [
      {
        path: "mainpage/",
        element: <MainPage />,
      },
    ],
  },
]);

ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <RouterProvider router={router} />
  </React.StrictMode>
);

Nav component:

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

function Nav(props) {
  return (
    <header className="header">
      <div className="header-container">
        <nav className="header-nav">
          <ul className="nav-list">
            <li>
              <Link to={`mainpage/`}>Home</Link>
            </li>
            <li>
              <Link to={`News/`}>News</Link>
            </li>
            <li>
              <Link to={`About/`}>About</Link>
            </li>
            <li>
              <Link to={`Contacts`}>Contacts</Link>
            </li>
          </ul>
        </nav>
      </div>
    </header>
  );
}

export default Nav;

Main page component:

import React from 'react';

function MainPage(props) {
  console.log('hi')

  return (
    <div>
      <div className="hello">
        hello world
      </div>
    </div>
  );
}

export default MainPage;

I've read the entire page of the react-router for beginners documentation, but I still can't solve this issue. I've tried all that I could.

英文:

I am gonna create react router structure in my React JS app. I have an issue. The issue is I can't use react-router element. So I have a component Main Page. I want to display after page is loaded. Also, I have nav component I want display it all the time. But I can see only nav component. Main Page component is found. I don't have any errors, but I can't see it. I can't see its content.

Index js file. Main file in my app

import React from &#39;react&#39;;
import {
  createBrowserRouter,
  RouterProvider,
} from &quot;react-router-dom&quot;;
import MainPage from &quot;./components/MainPage&quot;;
import ReactDOM from &#39;react-dom/client&#39;;
import Nav from &quot;./components/Nav&quot;;

const router = createBrowserRouter([
  {
    path: &quot;/&quot;,
    element: &lt;Nav /&gt;,
    children: [
      {
        path: &quot;mainpage/&quot;,
        element: &lt;MainPage /&gt;,
      },
    ],
  },
]);

ReactDOM.createRoot(document.getElementById(&quot;root&quot;)).render(
  &lt;React.StrictMode&gt;
    &lt;RouterProvider router={router} /&gt;
  &lt;/React.StrictMode&gt;
);

Nav component:

import React from &#39;react&#39;;
import { Link } from &quot;react-router-dom&quot;;

function Nav(props) {
  return (
    &lt;header className=&quot;header&quot;&gt;
      &lt;div className=&quot;header-container&quot;&gt;
        &lt;nav className=&quot;header-nav&quot;&gt;
          &lt;ul className=&quot;nav-list&quot;&gt;
            &lt;li&gt;
              &lt;Link to={`mainpage/`}&gt;Home&lt;/Link&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;Link to={`News/`}&gt;News&lt;/Link&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;Link to={`About/`}&gt;About&lt;/Link&gt;
            &lt;/li&gt;
            &lt;li&gt;
              &lt;Link to={`Contacts`}&gt;Contacts&lt;/Link&gt;
            &lt;/li&gt;
          &lt;/ul&gt;
        &lt;/nav&gt;
      &lt;/div&gt;
    &lt;/header&gt;
  );
}

export default Nav;

Main page component:

import React from &#39;react&#39;;

function MainPage(props) {
  console.log(&#39;hi&#39;)

  return (
    &lt;div&gt;
      &lt;div className=&quot;hello&quot;&gt;
        hello world
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

export default MainPage;

I've read the entire page of the react-router for beginners documentation, but I still can't solve this issue. I've tried all that I could.

答案1

得分: 0

你应该查看Nested routes的工作原理 不能使用 React 路由元素

所以在你的情况下,你只是渲染你的 Nav 组件,因为你没有为你的路由子组件定义 "placeholder" (<Outlet>)。

你的结构必须类似于

const router = createBrowserRouter([
    {
        path: "/",
        element: <Layout />,
        children: [
            {
                path: "mainpage/",
                element: <MainPage />,
            },
        ],
    },
]);
// Layout.jsx
import { Outlet } from 'react-router-dom';
import Nav from "./components/Nav";

const Layout = () => {
    return (<>
        <Nav />
        <Outlet />
    </>)
}
英文:

You should check how Nested routes works 不能使用 React 路由元素

So in your case, you're just rendering your Nav component, because you've not defined the "placeholder" (&lt;Outlet&gt;) for your route children.

Your structure must be something like

const router = createBrowserRouter([
    {
        path: &quot;/&quot;,
        element: &lt;Layout /&gt;,
        children: [
            {
                path: &quot;mainpage/&quot;,
                element: &lt;MainPage /&gt;,
            },
        ],
    },
]);
// Layout.jsx
import { Outlet } from &#39;react-router-dom&#39;;
import Nav from &quot;./components/Nav&quot;;

const Layout = () =&gt; {
    return (&lt;&gt;
        &lt;Nav /&gt;
        &lt;Outlet /&gt;
    &lt;/&gt;)
}

huangapple
  • 本文由 发表于 2023年2月16日 16:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定