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的更多相关文章

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

  2. react看这篇就够了(react+webpack+redux+reactRouter+sass)

    本帖将对一下内容进行分享: 1.webpack环境搭建: 2.如何使用react-router: 3.引入sass预编译: 4.react 性能优化方案: 5.redux结合react使用: 6.fe ...

  3. React与ES6(四)ES6如何处理React mixins

    React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...

  4. 玩转 React 【第03期】:邂逅 React 组件

    上期回顾 前文我们讲解了 React 模板 JSX,接着我们继续来看看 React 组件又是如何工作的呢? 组件化开发到了今天已经是大家的共识,在 React 中,组件同样也是组成我们整个项目的基本单 ...

  5. 【React自制全家桶】一、Webstrom+React+Ant Design+echarts搭建react项目

    前言 一.React是Facebook推出的一个前端框架,之前被用于著名的社交媒体Instagram中,后来由于取得了不错的反响,于是Facebook决定将其开源.出身名门的React也不负众望,成功 ...

  6. react 16.8版本新特性以及对react开发的影响

    Facebook团队对社区上的MVC框架都不太满意的情况下,开发了一套开源的前端框架react,于2013年发布第一个版本. react最开始倡导函数式编程,使用function以及内部方法React ...

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

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

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

随机推荐

  1. 使用JOTM实现分布式事务管理(多数据源)

    使用spring和hibernate可以很方便的实现一个数据源的事务管理,但是如果需要同时对多个数据源进行事务控制,并且不想使用重量级容器提供的机制的话,可以使用JOTM达到目的. JOTM的配置十分 ...

  2. Vue 学习记录<1>

    1.环境搭建:(前提node.js搭建) # 全局安装 vue-cli $ npm install --global vue-cli   # 创建一个基于 webpack 模板的新项目 $ vue i ...

  3. android设置Activity背景色为透明的3种方

    方法一:这种方法比较简单,只有一个步骤,只需要在配置文件中把需要设置为透明的activity的样式设置为 Android:theme="@android:style/Theme.Transl ...

  4. git仓库搭建

    第一步安装git [root@Centos-node2 ~]# yum -y install git 第二步创建git用户 [root@Centos-node2 ~]# useradd git [ro ...

  5. Mongodb总结1-启动和Shell脚本

    2013年,还在秒针,当时听说了Mongodb,就学习了下,搞了下HelloWorld.主要是熟悉Mongodb的启动.命令行的Shell脚本.Java访问的CRUD. 今天,由于需要,再次回顾和进一 ...

  6. c++中的相对路径

           今天在vs2010里读取相对路径下的图片文件出了点问题.于是查了一下相对路径的编程知识,记录下来分享给大家:      问题描写叙述:path=".\\TrainData\\& ...

  7. WCF REST (一)

    最近工作中学习使用了WCF REST,REST 有很多好处 高效 简约  面向资源  而客户端调用 也变得非常简单.REST 入门的资料等 大家可以去网上找 这里主要分享下遇到的问题以及解决~ 一.环 ...

  8. python基础-合并列表

    1.append()  向列表尾部追加一个新元素,列表只占一个索引位,在原有列表上增加 2.extend() 向列表尾部追加一个列表,将列表中的每个元素都追加进来,在原有列表上增加 3.+  直接用+ ...

  9. UVA 11178 - Morley's Theorem 向量

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. 【z12】&&【b092】hankson的趣味问题

    描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解了 ...