[英]BrowserRouter, Route, Router, Routes not working
I am learning to react and trying to build a nav bar.我正在学习做出反应并尝试建立一个导航栏。 But when I tried to use route to make my navbar links functional or add pages.
但是当我尝试使用路由使我的导航栏链接功能或添加页面时。 It shows me a blank page.
它向我显示了一个空白页。 But when I delete this portion
<BrowserRouter>...</BrowserRouter>
.但是当我删除这部分
<BrowserRouter>...</BrowserRouter>
。 My nav bar shows but as I don't use route there it won't show page components when I go to that page (ie http://localhost:3000/about).我的导航栏显示,但由于我不在那里使用路由,当我转到该页面时它不会显示页面组件(即 http://localhost:3000/about)。
Code:代码:
import React, { Component } from 'react';
import './App.css';
import 'whatwg-fetch';
import Series from '../../containers/Series';
import Navbar from '../Navbar';
import {BrowserRouter, Route, Router, Routes} from 'react-router-dom';
import About from '../../pages/about';
import Home from '../../pages/home';
function App() {
return (
<>
<Navbar />
<BrowserRouter>
<Routes>
<Route exact path="/" component={Home} />
<Route exact path="/about" component={About} />
</Routes>
</BrowserRouter>
</>
);
}
export default App;
Did you read the docs of this library ?你读过这个图书馆的文档吗?
BrouserRouter
is used to keep your UI in sync with the URL. BrouserRouter
用于使您的 UI 与 URL 保持同步。
You have to read guide section first of all.您必须首先阅读指南部分。 If you look carefully you see that you must use
Switch
component like a wrapper for your Route
's instead of Routes
.如果你仔细看,你会发现你必须使用
Switch
组件,就像你的Route
的包装器一样,而不是Routes
。
In your situation it must looks like在您的情况下,它必须看起来像
<>
<Navbar />
<BrowserRouter>
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/about" component={About} />
</Switch>
</BrowserRouter>
</>
A Switch
looks through its children Route
's and renders the first one that matches the current URL. Switch
查看其子Route
并呈现与当前 URL 匹配的第一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.