javascript 我的react-app没有渲染任何单个元素,但它成功编译,没有错误

wdebmtf2  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(63)

indes.js(npm启动此文件)

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import Routes from './routes';

ReactDOM.render(
  <Router>
    <Routes />
  </Router>,
  document.getElementById('root') // Use getElementById with the id attribute of the target element
);

用于路由的routes.js文件

import React from 'react';
import { Routes ,Route } from 'react-router-dom';
import Home from './components/Home';
import About from './components/About';
import Contact from './components/Contacts';
import NotFound from './components/NotFound';

const Rooutes = () => {
   return (
     <Routes>
       <Route exact path="/" element={Home} />
       <Route path="/about" element={About} />
       <Route path="/contact" element={Contact} />
       <Route element={NotFound} />
     </Routes>
   );
};
 
export default Rooutes;

和一个html文件的使用的,像这样:

import React from 'react';

const About = () => {
 return (
   <>
     h1 about Component
   </>
 );
};

export default About;

一切都很完美没有检测到错误,npm编译成功,但没有html元素呈现给浏览器,任何建议/帮助?

gfttwv5a

gfttwv5a1#

如果你这样做,它会起作用

<Route exact path="/" element={<Home/>} />

相关问题