[React Router v4] Conditionally Render a Route with the Switch Component
We often want to render a Route conditionally within our application. In React Router v4, the Route components match the current route inclusively so a “stack” of Routes will all be processed. To render a single Route exclusively we can wrap them in the Switch component to render the first Route that matches our current URL.
<Router basename={props.path}><div><Links /><Route exact path="/" render={() => <h1>Home</h1>} /><Route path="/about" render={() => <h1>About</h1>} /><Route path="/contact" render={() => <h1>Contact</h1>} /><Route path="/:itemid"render={({match}) => <h1>Item: {match.params.itemid}</h1>} /></div></Router>
Consider this part of code, we want nav to '/about' path, it will show both about page and itemid page, because it consider /about is part of '/:itemid', to solve this problem, we can introduce 'Switch', it will render first match Route.
// https://jsbin.com/qijilugimport React from 'react'; import {BrowserRouter as Router,Route,Link,Switch } from 'react-router-dom';const Links = () =><nav><Link to="/">Home</Link><Link to="/about">About</Link><Link to="/contact">Contact</Link></nav>const App = (props) => (<Router basename={props.path}><div><Links /><Switch><Route exact path="/" render={() => <h1>Home</h1>} /><Route path="/about" render={() => <h1>About</h1>} /><Route path="/contact" render={() => <h1>Contact</h1>} /><Route path="/:itemid"render={({match}) => <h1>Item: {match.params.itemid}</h1>} /></Switch></div></Router> )export default App
转载于:https://www.cnblogs.com/Answer1215/p/6596330.html
[React Router v4] Conditionally Render a Route with the Switch Component相关推荐
- [React Router v4] Intercept Route Changes
If a user has entered some input, or the current Route is in a "dirty" state and we want t ...
- hitchhiker部署_Hitchhiker的React Router v4指南:无限远的递归路径!
hitchhiker部署 Welcome to the third part of the Hitchhiker's Guide to React Router v4. In this article ...
- hitchhiker部署_《 Hitchhiker的React Router v4指南》:路由配置的隐藏值
hitchhiker部署 Welcome to the Hitchhiker's Guide to React Router v4, Part IV! 欢迎来到< React Router v4 ...
- 关于React Router v4的虚张声势指南
In this article, we'll cover the important things you can quickly learn to be informed and confident ...
- 使用React Router v4的嵌套路由
React Router v4 introduced a new declarative, component based approach to routing. With that approac ...
- react router v4 简介
最近使用react router 遇到一个问题:当对某个路由添加参数的时候/list/:app 这列路由,点击这个路由以后再点击其他路由,location地址就追加到后面,问不是replace. /l ...
- React 折腾记 - (1) React Router V4 和antd侧边栏的正确关联及动态title的实现
前言 一如既往,实战出真理. 有兴趣的可以瞧瞧,没兴趣的大佬请止步于此. 免得浪费您的时间 效果图 基于antd的sidebar组件封装 折腾记的技术栈选型 Mobx & mobx-react ...
- React 16.x折腾记 - (1) React Router V4 和antd侧边栏的正确关联及动态title的实现
前言 一如既往,实战出真理,有兴趣的可以瞧瞧,没兴趣的大佬请止步于此. 效果图 基于antd的sidebar组件封装 折腾记的技术栈选型 Mobx & mobx-react(用起来感觉良好的状 ...
- hitchhiker部署_Hitchhiker的React Router v4指南:20分钟内完成Grok React Router
hitchhiker部署 Hi fellow React Hitchhiker! Want a ride into React Router? Jump in. Let's go! 大家好,React ...
最新文章
- 解决JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer问题
- Nginx 笔记与总结(15)nginx 实现反向代理 ( nginx + apache 动静分离)
- C++自定义非极大值抑制(Canny边缘检测,亚像素方法)
- ZNZD平台vue项目
- 求“厉害”数 (10 分)
- 初识 Vue(01)---(输出 Hello World)
- springcloud断点续传源码_两套SpringCloud版的开源项目,项目源码和教程齐全
- Faker 快速构造测试数据
- linux gdb基本概念
- 使用JSONP解决同源限制问题
- 聊聊spring data jpa的JpaQueryLookupStrategy
- 高斯核原理详解+生成高斯核的Python代码
- pos 指令集 linux,Linux系统下10大开源POS系统
- HTML(超文本标记语言)
- 【有利可图网】PS实战教程43:PS合成沙尘暴侵蚀打散场景的灰姑娘图片
- 什么是嵌入式系统,什么是单片机?
- java多线程学习笔记--一.多线程的基础知识
- 论文写作 1: 学术论文的基本概念
- 阿里云配置域名解析(全过程)
- AutoSAR系列讲解(实践篇)11.4-NvBlockSwComponents(上)