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相关推荐

  1. [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 ...

  2. hitchhiker部署_Hitchhiker的React Router v4指南:无限远的递归路径!

    hitchhiker部署 Welcome to the third part of the Hitchhiker's Guide to React Router v4. In this article ...

  3. hitchhiker部署_《 Hitchhiker的React Router v4指南》:路由配置的隐藏值

    hitchhiker部署 Welcome to the Hitchhiker's Guide to React Router v4, Part IV! 欢迎来到< React Router v4 ...

  4. 关于React Router v4的虚张声势指南

    In this article, we'll cover the important things you can quickly learn to be informed and confident ...

  5. 使用React Router v4的嵌套路由

    React Router v4 introduced a new declarative, component based approach to routing. With that approac ...

  6. react router v4 简介

    最近使用react router 遇到一个问题:当对某个路由添加参数的时候/list/:app 这列路由,点击这个路由以后再点击其他路由,location地址就追加到后面,问不是replace. /l ...

  7. React 折腾记 - (1) React Router V4 和antd侧边栏的正确关联及动态title的实现

    前言 一如既往,实战出真理. 有兴趣的可以瞧瞧,没兴趣的大佬请止步于此. 免得浪费您的时间 效果图 基于antd的sidebar组件封装 折腾记的技术栈选型 Mobx & mobx-react ...

  8. React 16.x折腾记 - (1) React Router V4 和antd侧边栏的正确关联及动态title的实现

    前言 一如既往,实战出真理,有兴趣的可以瞧瞧,没兴趣的大佬请止步于此. 效果图 基于antd的sidebar组件封装 折腾记的技术栈选型 Mobx & mobx-react(用起来感觉良好的状 ...

  9. 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 ...

最新文章

  1. 解决JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer问题
  2. Nginx 笔记与总结(15)nginx 实现反向代理 ( nginx + apache 动静分离)
  3. C++自定义非极大值抑制(Canny边缘检测,亚像素方法)
  4. ZNZD平台vue项目
  5. 求“厉害”数 (10 分)
  6. 初识 Vue(01)---(输出 Hello World)
  7. springcloud断点续传源码_两套SpringCloud版的开源项目,项目源码和教程齐全
  8. Faker 快速构造测试数据
  9. linux gdb基本概念
  10. 使用JSONP解决同源限制问题
  11. 聊聊spring data jpa的JpaQueryLookupStrategy
  12. 高斯核原理详解+生成高斯核的Python代码
  13. pos 指令集 linux,Linux系统下10大开源POS系统
  14. HTML(超文本标记语言)
  15. 【有利可图网】PS实战教程43:PS合成沙尘暴侵蚀打散场景的灰姑娘图片
  16. 什么是嵌入式系统,什么是单片机?
  17. java多线程学习笔记--一.多线程的基础知识
  18. 论文写作 1: 学术论文的基本概念
  19. 阿里云配置域名解析(全过程)
  20. AutoSAR系列讲解(实践篇)11.4-NvBlockSwComponents(上)

热门文章

  1. Hibernate 关联映射 之 多对多 关联(一)
  2. android ViewPager页面左右滑动切换
  3. 八数码 poj 1077 广搜 A* IDA*
  4. golang中的目录操作
  5. 开发相关手册、STM32各种库文件、相关软件、工具连接等(不断更新)
  6. springboot-web开发(rest风格支持)
  7. java开发,年薪15W的你和年薪50W的他的差距
  8. Callback到Promise再到Async进化初探
  9. 软件测试之三——路径覆盖
  10. Redhat7 systemctl命令