reactjs - 反应路线不适用于自定义导航栏
问题描述
我正在使用在 NPM 上找到的自定义导航栏库来呈现我的导航栏,但是navlinks
自定义导航栏中的导航栏不起作用。当我单击链接时,地址栏中的路由会更新,但不会呈现组件。虽然使用简单列表时,我能够渲染所有路线。
function App() {
return (
<React.Fragment>
/* This custom navbar is not working for routing */
<Navbar></Navbar>
/* This list is working fine for routing */
{/* <ul>
<li>
<NavLink to='/'>Home</NavLink>
</li>
<li>
<NavLink to='/about'>About</NavLink>
</li>
<li>
<NavLink to='/contact'>Contact</NavLink>
</li>
</ul> */}
<Switch>
<Route exact path='/' component={Home} />
<Route path='/about' component={About} />
<Route path='/contact' component={Contact} />
</Switch>
</React.Fragment>
);
}
这是我正在使用的自定义导航栏组件:自定义导航栏
解决方案
您可能会忘记用
<Router> or <BrowserRouter>
.
试一下:
import {BrowserRouter, Router} from "react-router-dom";
<BrowserRouter>
<React.Fragment>
...
</React.Fragment>
</BrowserRouter>
或者
<Router>
<React.Fragment>
...
</React.Fragment>
</Router>
推荐阅读
- typescript - 为@typescript-eslint/parser 设置了“parserOptions.project”
- angular - ads.txt 应该放在 Angular 8 SSR 中的什么位置?
- awk - awk 将匹配的字段更改为另一个
- r - 通过 Shinyapps.io 连接到 MongoDB Atlas
- .net-core - 无法加载文件或程序集 Microsoft.Extensions.Logging.Abstractions
- javascript - 有没有办法劫持 DOM 中的内容?
- python - 熊猫滚动总和分别乘以值
- excel - 尝试使用 VBA 在 Col 中插入 IFERROR 公式
- ios - Apple Developer - 注册 App ID 下的“字体”功能是什么?
- ios - 为什么 iOS 只为 FaceID 显示权限警报?