简体   繁体   English

反应路由不加载页面

[英]React routing not loading pages

Hi i am learning react and i got stuck on a problem.嗨,我正在学习反应,但我遇到了一个问题。 i have started working on routing but its not working at all.我已经开始研究路由,但它根本不起作用。 Code:代码:

 import {Route} from 'react-router-dom'
import AllMeetupsPage from './pages/AllMeetups';
import NewMeetupsPage from './pages/NewMeetups';
import FavoritesPage from './pages/Favorites';


function App() {
  return (
      <div>
        <Route path = '/'>
          <AllMeetupsPage />
        </Route>
        <Route path = '/new-meetup'>
          <NewMeetupsPage />
        </Route>
        <Route path = '/favorites'>
          <FavoritesPage />
        </Route>
      </div>
  );
}

export default App;

i have a blank screen even though the pages should load.即使页面应该加载,我也有一个空白屏幕。 Each page when open should only print one line just to see that i can see routing working however all i see is a blank space meaning its not working correctly.You can view the full code here: https://github.com/Kroplewski-M/Routing thank you in advance打开时的每一页应该只打印一行,只是为了看到我可以看到路由工作,但是我看到的只是一个空格,这意味着它不能正常工作。你可以在这里查看完整的代码: https://github.com/Kroplewski- M /路由提前谢谢你

You are not wrapping the route under the router that's why your route is not working.您没有将路由包装在路由器下,这就是您的路由不起作用的原因。

Here is the code.这是代码。

 import {Router,Route} from 'react-router-dom'
 import AllMeetupsPage from './pages/AllMeetups';
 import NewMeetupsPage from './pages/NewMeetups';
 import FavoritesPage from './pages/Favorites';

 function App() {
   return (
       <div>
        <Router>
         <Route path = '/'>
           <AllMeetupsPage />
         </Route>
         <Route path = '/new-meetup'>
           <NewMeetupsPage />
         </Route>
         <Route path = '/favorites'>
           <FavoritesPage />
         </Route>
       </Router>
       </div>
   );
 }

 export default App;

I hope it will work.我希望它会奏效。

You have to wrap your routes with <BrowserRouter> which is done already in index file which is fine.您必须使用<BrowserRouter>包装您的路线,这已经在index文件中完成,这很好。 Next, Routes must be wrapped with <Routes> component as of 'react-router-dom' latest version v6接下来,必须使用<Routes>组件包装路由,从'react-router-dom'最新版本v6

import { Routes, Route } from "react-router-dom";

function App(){
return (
<div>
  <Routes>
      <Route path="/" element={<AllMeetupsPage />} />
      <Route path="/new-meetup" element={<NewMeetupsPage />} /> 
  </Routes>
</div>
);
}

export default App;

Simply you can handle the routing in your index.js like this:只需像这样处理 index.js 中的路由:

import { BrowserRouter, Route, Routes } from "react-router-dom";

ReactDOM.render(
  <div>
  <BrowserRouter>
    <Routes>
   
      <Route path="/" element={<AllMeetupsPage />} />
      <Route path="/new-meetup" element={<NewMeetupsPage />} />
      <Route path="/favorites" element={<FavoritesPage />} />
     
   
    </Routes>
  </BrowserRouter>
  </div>,
  document.getElementById("root")
);

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

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