【问题标题】:React Router v6 - not renderingReact Router v6 - 不渲染
【发布时间】:2023-02-06 02:20:58
【问题描述】:

我正在学习 React。我不明白为什么 react router v6 不渲染任何组件。

源代码/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { Customers, Invoices, Jobs, Login, Sales, } from './modules'

import './App.css';
import './index.css';

import App from './App';

const rootElement = document.getElementById("root");
ReactDOM.render(
    <BrowserRouter>
        <Routes>
            <Route path="/" element={<App />}>
                <Route path="/jobs" element={<Jobs />} />
                <Route path="customers" element={<Customers />} />
                <Route path="invoices" element={<Invoices />} />
                <Route path="sales" element={<Sales />} />
                <Route
                path="*"
                element={
                    <main style={{ padding: "1rem" }}>
                    <p>There's nothing here!</p>
                    </main>
                }
                />
            </Route>
        </Routes>
    </BrowserRouter>,
    rootElement
);

src/modules/invoices/Invoices.js

import { Link } from "react-router-dom";
import { getInvoices } from "../../data";

export default function Invoices() {
  let invoices = getInvoices();
  return (
    <div style={{ display: "flex" }}>
      <nav
        style={{
          borderRight: "solid 1px",
          padding: "1rem",
        }}
      >
        {invoices.map((invoice) => (
          <Link
            style={{ display: "block", margin: "1rem 0" }}
            to={`/invoices/${invoice.number}`}
            key={invoice.number}
          >
            {invoice.name}
          </Link>
        ))}
      </nav>
    </div>
  );
}

data.js 取自 react-router 文档 https://reactrouter.com/docs/en/v6/getting-started/tutorial

我做错了什么?

【问题讨论】:

  • invoices 与 /invoices 在你的路线中怎么样?
  • 我看不到任何明显的错误,控制台中是否有任何错误/警告?
  • 你有任何错误吗?你的进口搞砸了。必须是import Invoices from './modules/invoices/Invoices'
  • @Art 没有错误。进口没问题。在 ./modules 我有 Index.js 文件导出像export { default as Invoices } from './invoices/Invoices';
  • 您可以在这里添加 App 组件吗?它需要有一个奥特莱斯,如果你的进口商品没问题,其他一切似乎都很好,那一定是问题所在

标签: javascript reactjs


【解决方案1】:

需要在组件中添加。我希望应用程序组件能够被渲染,我们必须提到我们必须渲染子组件的占位符。

【讨论】:

    【解决方案2】:
    ReactDOM.render(
        <BrowserRouter>
            <Routes>
                <Route path="/" element={<App />}/>
                <Route path="/jobs" element={<Jobs />} />
                <Route path="/customers" element={<Customers />} />
                <Route path="/invoices" element={<Invoices />} />
                <Route path="/sales" element={<Sales />} />
                <Route
                    path="*"
                    element={
                        <main style={{ padding: "1rem" }}>
                        <p>There's nothing here!</p>
                        </main>
                    }
                />
           
            </Routes>
        </BrowserRouter>,
        rootElement
    );
    

    我发现了这些错误,

    1. 您已将所有组件包含在单个路由中。

      或者如果您希望所有内容都在您的组件中。 然后在 App 组件本身中初始化路由器。 你可以参考这个 https://github.com/NaveenkumarMD/cryptera-website/blob/master/src/App.js

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 2022-01-20
    • 2021-12-28
    • 2016-02-22
    • 2018-09-20
    • 2020-09-04
    • 1970-01-01
    相关资源
    最近更新 更多