简体   繁体   English

反应路由器组件未显示

[英]React Router Components not showing

I'm having an issue with react-router-dom, the components aren't showing up.我遇到了 react-router-dom 的问题,组件没有出现。 I have checked in package.json that react-router-dom is well installed, and it is as it is showing:我已经在 package.json 中检查了 react-router-dom 安装良好,它显示的是:

"react-router-dom": "^6.3.0",

And here is my App这是我的应用程序

import React from "react";
import Home from "./Home.js"
import More from "./More.js"
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'


function App() {
  return (
      <Router>
        <Routes>
          <Route exact path="/" element={<Home/>}/>
          <Route exact path="/more" element={<More/>}/>
        </Routes>
      </Router>
  );
}

export default App;

I feel like im doing everything right as i also added this to my index.js我觉得我做的一切都是正确的,因为我也将它添加到我的 index.js

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  document.getElementById('root')
);

But my components are still not showing up但是我的组件仍然没有出现

The issue is that you can not render a router inside another router, and you have:问题是您不能在另一个路由器内渲染路由器,并且您有:

 <BrowserRouter>
    <App />
  </BrowserRouter>

wich is:这是:

 <BrowserRouter>
    <Router>
        <Routes>
          <Route exact path="/" element={<Home/>}/>
          <Route exact path="/more" element={<More/>}/>
        </Routes>
      </Router>
</BrowserRouter>

Try to remove one of the routers尝试删除其中一个路由器

You can remove the router (BrowserRouter) in the app.js.您可以在 app.js 中删除路由器(BrowserRouter)。 Keep the in the index.js and the app will work.保留在 index.js 中,应用程序将运行。

App.js file应用程序.js 文件

import React from "react";
import Home from "./Home.js"
import More from "./More.js"
import { Route, Routes } from 'react-router-dom'


function App() {
  return (
        <Routes>
          <Route exact path="/" element={<Home/>}/>
          <Route exact path="/more" element={<More/>}/>
        </Routes>
  );
}

export default App;

Index.js file索引.js 文件

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  document.getElementById('root')
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM