在我的React项目中,路由器工作不正常。

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

In my React Project Router doesn't work well

问题

请尝试将 Main 组件直接渲染到 createRoot 中,替换 <App />

英文:

Would you please help me solve my problem in my react router?
I have made three pages: App.js index.js Main.js, and some pages at y src/pages as Home, About, Contact,...

index.js:

import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App";

createRoot(document.getElementById("root")).render(<App />);

App.js:

import React from "react";
import "./App.css";
import Navbar from "./Navbar";
import Header from "./header";
import Footer from "./footer";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap/dist/js/bootstrap.js";

export default function App() {
  return (
    <div className="App">
      <Header />
     <Navbar />
      <Footer />
    </div>
  );
}

Main.js:

import React from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import About from "./pages/aboutus";
import Contact from "./pages/contactus";
import Services from "./pages/depressionadults";
import Home from "./pages/Home";
import Header from "./header";
import Navbar from "./Navbar";
import Footer from "./footer";

export default function Main() {
  return (
    <BrowserRouter>
    <Header />
    <Navbar />
      <Routes>
        <Route exact path="/" element={<Home />}>
          <Route path="/pages/depressionadults" element={<Services />} />
          <Route path="/pages/App" element={<About />} />
          <Route path="/pages/contactus" element={<Contact />} />
        </Route>
      </Routes>
      <Footer />
    </BrowserRouter>
  );
}


But just URL address changes and nothing happens about loading the components I've addressed to. It seems that App.js is not necessary and I should render <Main /> directly, but when I clear it nothing works at all. If I should do any changes in my index.html too?

I expect my Router works well, and I cannot find out where is the problem?

答案1

得分: -1

你正在使用错误的组件,在index.js文件中应该使用<Main/>组件而不是<App/>

英文:

You are using the wrong component,
put &lt;Main/&gt; component instead of &lt;App/&gt; in index.js file.

huangapple
  • 本文由 发表于 2023年5月11日 01:42:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221256.html
匿名

发表评论

匿名网友

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

确定