[React Router v4] Create Basic Routes with the React Router v4 BrowserRouter
React Router 4 has several routers built in for different purposes. The primary one you will use for building web applications is the BrowserRouter. In this lesson you will import the BrowserRouter and create some basic Route components.
After create your app with 'creat-react-app', we going to install the react-router-dom:
- npm i -D react-router-dom@next
Import BrowserRouter:
- import {
- BrowserRouter as Router,
- Route
- } from 'react-router-dom';
Using Router:
- <Router>
- <div>
- <Route exact path="/" component={App}></Route>
- <Route path="/about" component={About}></Route>
- <Route
- strict
- path="/about/"
- render={() => <h2>About render</h2>}></Route>
- <Route
- path="/demo"
- children={({match}) => match && <h2>demo</h2>}></Route>
- </div>
- </Router>
Here we use three different ways to render a component or Html to the DOM:
1. Using Component:
- <Route exact path="/" component={App}></Route>
- <Route path="/about" component={About}></Route>
Here the 'exact' flag tells that it should match only '/'.
2. Using render:
we can just render some html:
- <Route
- strict
- path="/about/"
- render={() => <h2>About render</h2>}></Route>
3. Using children:
- <Route
- path="/demo"
- children={({match}) => match && <h2>demo</h2>}></Route>
By default what we write into 'children' will be rendered no matter which path it matchs.
If for example, we only want it to be shown when match '/demo', we can check whether there is a 'match' object exists on props.
[React Router v4] Create Basic Routes with the React Router v4 BrowserRouter的更多相关文章
- Wait… What Happens When my React Native Application Starts? — An In-depth Look Inside React Native
Discover how React Native functions internally, and what it does for you without you knowing it. Dis ...
- react看这篇就够了(react+webpack+redux+reactRouter+sass)
本帖将对一下内容进行分享: 1.webpack环境搭建: 2.如何使用react-router: 3.引入sass预编译: 4.react 性能优化方案: 5.redux结合react使用: 6.fe ...
- React与ES6(四)ES6如何处理React mixins
React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...
- 玩转 React 【第03期】:邂逅 React 组件
上期回顾 前文我们讲解了 React 模板 JSX,接着我们继续来看看 React 组件又是如何工作的呢? 组件化开发到了今天已经是大家的共识,在 React 中,组件同样也是组成我们整个项目的基本单 ...
- 【React自制全家桶】一、Webstrom+React+Ant Design+echarts搭建react项目
前言 一.React是Facebook推出的一个前端框架,之前被用于著名的社交媒体Instagram中,后来由于取得了不错的反响,于是Facebook决定将其开源.出身名门的React也不负众望,成功 ...
- react 16.8版本新特性以及对react开发的影响
Facebook团队对社区上的MVC框架都不太满意的情况下,开发了一套开源的前端框架react,于2013年发布第一个版本. react最开始倡导函数式编程,使用function以及内部方法React ...
- [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 ...
- [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 ...
- [React Router] Create a ProtectedRoute Component in React Router (setState callback to force update)
In this lesson we'll create a protected route just for logged in users. We'll combine a Route with a ...
随机推荐
- manjaro安装virtualbox教程
安装前需要知道 你需要知道你当前的内核版本 uname -r,比如输出了4.14.20-2-MANJARO那么你的内核版本为414 安装VirtualBox sudo pacman -S virtua ...
- TextView-属性大全(设置超链接颜色)
今天想要修改一个textview下的超链接的颜色值,自己当时在网上搜了一下,结果看到的全是怎么给一个textview中的部分内容设置颜色.下划线等.当时就以为在textview属性里面可能不存在设定超 ...
- elasticsearch cluster 概述
在源码概述中我们分析过,elasticsearch源码从功能上可以分为分布式功能和数据功能,接下来这几篇会就分布式功能展开.这里首先会对cluster作简单概述,然后对cluster所涉及的主要功能详 ...
- Android 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar
package cc.testprogressdialog; import android.os.Bundle; import android.view.Gravity; import android ...
- css基础属性
color:设置文本颜色:属性值:1.表示颜色的英文单词,例如:red.blue.green.pink.purple.cyan等:2.十六进制表示法:#ff0000: 0.1.2...9.a.b.c. ...
- sublime找到成对标签(Ctrl+Shift+")
sublime找到成对标签(Ctrl+Shift+") windows版本默认快捷键是Ctrl+Shift+" sublime text怎么突出显示成对标签 使用BracketHi ...
- IQMath是什么 浮点转定点运算,dsp
[转帖注明出处:blog.csdn.net/lanmanck] 网上搜了一下没发现非常合适的,特写出来与大家分享. 大家都知道嵌入式系统里带浮点运算指令的CPU都比較少,TI的DSP也是定点的廉价. ...
- BZOJ 2245 SDOI 2011 工作安排 费用流
题目大意:有一些商品须要被制造.有一些员工.每个员工会做一些物品,然而这些员工做物品越多,他们的愤慨值越大,这满足一个分段函数.给出哪些员工能够做哪些东西,给出这些分段函数,求最小的愤慨值以满足须要被 ...
- openCV 和GDI画线效率对比
一. 由于项目需要,原来用GDI做的画线的功能,新的项目中考虑到垮平台的问题,打算用openCV来实现,故此做个效率对比. 二. 2点做一条线,来测试效率. 用了同样的画板大小---256*256的大 ...
- 关于spring获取webApplication.getBean多种途径和简单解释
ApplicationContext ac1 = new FileSystemXmlApplicationContext("com/spark/system/applicationConte ...