简体   繁体   English

React Router v6 不渲染任何东西

[英]React Router v6 not rendering anything

when using react-router-dom, nothing rednerr in the App (blank page), my components work without the routing (commented in the code).使用 react-router-dom 时,应用程序中没有任何 rednerr(空白页),我的组件在没有路由的情况下工作(在代码中注释)。 I have no Idea why this is happening.我不知道为什么会这样。

ps i did not include the {jobs} object but that is irrelevant to the problem. ps 我没有包括 {jobs} object 但这与问题无关。

Heres my code:这是我的代码:

import React from 'react';
import {
  BrowserRouter as Router,
  Routes,
  Route,
} from "react-router-dom";
import List from './components/list/List';
import Navbar from './components/navbar/Navbar';

function App() {  
//  return (
//   <>
//     <Navbar />
//     <div className='w-full flex flex-col content-center items-center'>
//       <List jobs={jobs} />
//     </div>
//   </>
//  )
  return (
    <Router>
      <div>
        <Navbar />
        <Routes>
          <Route path="/contact">
          </Route>
          <Route path="/company/:id">
          </Route>
          <Route path="/companies">
          </Route>
          <Route exact path="/">
              <div className='w-full flex flex-col content-center items-center'>
                <List jobs={jobs} />
              </div>
          </Route>
        </Routes>
      </div>  
    </Router>     
  )
}

export default App

with React Router V6, the content of the route should be passed by the property element .使用 React Router V6,路由的内容应该通过属性element传递。

See example below:请参见下面的示例:


import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import List from './components/list/List';
import Navbar from './components/navbar/Navbar';

function App() {
  //  return (
  //   <>
  //     <Navbar />
  //     <div className='w-full flex flex-col content-center items-center'>
  //       <List jobs={jobs} />
  //     </div>
  //   </>
  //  )
  return (
    <Router>
      <div>
        <Navbar />
        <Routes>
          <Route path="/contact"></Route>
          <Route path="/company/:id"></Route>
          <Route path="/companies"></Route>
          <Route
            exact
            path="/"
            element={
              <div className="flex w-full flex-col content-center items-center">
                <List jobs={jobs} />
              </div>
            }
          />
        </Routes>
      </div>
    </Router>
  );
}

export default App;

Routes need an element to know what to load.路由需要一个元素来知道要加载什么。

<Route path="/contact"> </Route>

Should be:应该:

<Route path={"/contact"} element={<Contact />} />

And the element being called:被调用的元素:

export function Contact(){
  return <div>Contact us</div>
}

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

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