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/qijilug

import 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

[React Router v4] Conditionally Render a Route with the Switch Component的更多相关文章

  1. [React Router v4] Render Multiple Components for the Same Route

    React Router v4 allows us to render Routes as components wherever we like in our components. This ca ...

  2. [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 to confirm ...

  3. [React Router v4] Render Catch-All Routes with the Switch Component

    There are many cases where we will need a catch-all route in our web applications. This can include ...

  4. [React Router v4] Render Nested Routes

    With React Router v4 the entire library is built as a series of React components. That means that cr ...

  5. [Web 前端] React Router v4 入坑指南

    cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...

  6. [React Router v4] Redirect to Another Page

    Overriding a browser's current location without breaking the back button or causing an infinite redi ...

  7. [React Router v4] Parse Query Parameters

    React Router v4 ignores query parameters entirely. That means that it is up to you to parse them so ...

  8. React Router V4发布

    React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...

  9. [React Router v4] Use Regular Expressions with Routes

    We can use regular expressions to more precisely define the paths to our routes in React Router v4. ...

随机推荐

  1. 洛谷 P2384 最短路

    洛谷 P2384 最短路 题目背景 狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗? 他会给你10000000000000000000000000000 ...

  2. Java经典23种设计模式之行为型模式(二)

    本文接着介绍行为型模式里的解释器模式.迭代器模式.中介者模式. 一.解释器模式Interpret 给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言的中的句子. 1 ...

  3. openGLES(一)

    准备工作 工具安装 jdk安装 sdk安装 IDE开发环境 一个手机 推荐使用真机,它可以反映真实的性能和结果. 虚拟机创建 Android SDK 执行SDK Manager.exe Tools-- ...

  4. "C:\Program Files\Internet Explorer\iexplore.exe" -extoff 无加载项启动IE 浏览器打开时全屏模式

    "C:\Program Files\Internet Explorer\iexplore.exe" -extoff 无加载项启动IE浏览器打开时全屏模式

  5. error LNK2001: unresolved external symbol "public: virtual

    1) Mine solution : project-setting :static lib to shared dll .then ok. 找不到secondchar的定义, 你是否没有把包含sec ...

  6. 无状态会话bean(1)---定义

    无状态会话bean用于完毕在单个方法的生命周期内的操作.无状态bean能够实现很多业务操作,可是每一个方法都不能假定不论什么其它的方法会在它之前调用.后半句的意思是如今的你可能不是刚才的你.明天的你可 ...

  7. amazeui学习笔记--js插件(UI增强)--警告框Alert

    amazeui学习笔记--js插件(UI增强)--警告框Alert 一.总结 1.警告框基本样式:用am-alert声明div容器, <div class="am-alert" ...

  8. 边缘独立(marginal independent)的理解及举例

    1. 定义 ∀xi∈dom(X),yj∈dom(Y),yk∈dom(Y),如果满足, P(X=xi|Y=yj)==P(X=xi|Y=yk)P(X=Xi) 则称随机变量 X 边缘独立于随机变量 Y. 理 ...

  9. js的AJAX请求有关知识总结

    什么是AJAX?AJAX作用是什么? async javascript and xml(异步的javascript和xml) 作用:实现局部刷新 async:我们真实项目中一般使用AJAX从服务器端获 ...

  10. (转)rman中关于archivelog操作

    转自:http://www.xifenfei.com/1609.html RMAN> list backup of archivelog all;列出所有archive log 备份 RMAN& ...