Route 是 React Router中用于配置路由信息的组件,每当有一个组件需要根据 URL 决定是否渲染时,就需要创建一个 Route。

1) path

每个 Route 都需要定义一个 path 属性,path 属性是一个url,当 URL 匹配一个 Route 时,这个 Route 中定义的组件就会被渲染出来。

2)Route 渲染组件的方式

(1)component

component 的值是一个组件,当 URL 和 Route 匹配时,Component属性定义的组件就会被渲染。例如:

<Route path='/foo' component={Foo} >

当 URL = "http://example.com/foo" 时,Foo组件会被渲染。

(2) render

render 的值是一个函数,这个函数返回一个 React 元素。这种方式方便地为待渲染的组件传递额外的属性。例如:

<Route path='/foo' render={(props) => {
<Foo {...props} data={extraProps} />
}}>
</Route>

Foo 组件接收了一个额外的 data 属性。

(3)children

children 的值也是一个函数,函数返回要渲染的 React 元素。 与前两种方式不同之处是,无论是否匹配成功, children 返回的组件都会被渲染。但是,当匹配不成功时,match 属性为 null。例如:

<Route path='/foo' render={(props) => {
<div className={props.match ? 'active': ''}>
<Foo {...props} data={extraProps} />
</div>
}}>
</Route>

如果 Route 匹配当前 URL,待渲染元素的根节点 div 的 class 将设置成 active.

React Router的Route的使用的更多相关文章

  1. [React] React Router: Route Parameters

    A router library is no good if we have to hardcode every single route in our application. In this le ...

  2. [React] React Router: Router, Route, and Link

    In this lesson we'll take our first look at the most common components available to us in react-rout ...

  3. 【react router路由】<Router> <Siwtch> <Route>标签

    博客 https://www.jianshu.com/p/ed5e56994f13?from=timeline 文档 http://react-guide.github.io/react-router ...

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

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

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

  7. [转] React Router 使用教程

    PS:react-route就是一个决定生成什么父子关系的组件,一般和layout结合起来,保证layout不行,内部的子html进行跳转 你会发现,它不是一个库,也不是一个框架,而是一个庞大的体系. ...

  8. [Redux] Navigating with React Router <Link>

    We will learn how to change the address bar using a component from React Router. In Root.js: We need ...

  9. [Redux] Adding React Router to the Project

    We will learn how to add React Router to a Redux project and make it render our root component. Inst ...

随机推荐

  1. 小程序中添加客服按钮contact-button

    小程序的客服系统,是微信做的非常成功的一个功能,开发者可以很方便的通过一行代码,就可实现客服功能. 1. 普通客服按钮添加 <button open-type='contact' session ...

  2. 用Python抓取网页并解析

    软件版本 python:2.7.12 网页抓取库 网页抓取库为requests, github地址为:https://github.com/requests/requests, 文档地址为:http: ...

  3. [UE4]镜像

    一.这是一个右手模型,通过镜像可以得到一个左右模型. 二.通过上图分析,镜面是X轴和Z轴形成的一个面,Y轴与XZ面垂直,因此就是镜像Y轴,将模型的Transform.Scale.Y设置为-1,即可得到 ...

  4. Centos7修改默认网卡名(改为eth0)以及网卡启动报错RTNETLINK answers: File exists处理

    安装好centos7版本的系统后,发现默认的网卡名字有点怪,为了便于管理,可以手动修改.下面对centos7版本下网卡重命名操作做一记录:1)编辑网卡信息[root@linux-node2~]# cd ...

  5. RESTful levels、HATEOAS

    概述: REST(英文:Representational State Transfer,简称REST)描述了一个架构样式的网络系统,比如 web 应用程序.它首次出现在 2000 年 Roy Fiel ...

  6. sqlserver 已星期一为第一天统计周

    本文来源:https://blog.csdn.net/sqlserverdiscovery/article/details/53080695 SELECT GETDATE() AS THEDAY, c ...

  7. 【原创】Mac book pro入手后,需要做哪些才能开始开展自动化测试工作

    2018国庆节,脑袋一热,入手了一台Mac book pro,从此掉坑到了这个异构的操作系统中,因为之前工作中接触了Windows.Linux.Unix等操作系统的诸多版本,基本的操作倒是不成问题,但 ...

  8. 设计模式<1>------单例模式和原型模式------创建型

    原文引自:http://www.cnblogs.com/lonelyxmas/p/3720808.html 单例模式 单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并 ...

  9. 【学习】Python进行数据提取的方法总结【转载】

    链接:http://www.jb51.net/article/90946.htm 数据提取是分析师日常工作中经常遇到的需求.如某个用户的贷款金额,某个月或季度的利息总收入,某个特定时间段的贷款金额和笔 ...

  10. c博客作业01--顺序、分支结构

    1.本章学习总结 1.1思维导图 1.2本章的学习体会及代码量学习体会 1.2.1学习体会 本周学习的感受:原先基础是0的,所以本周学习起来并不是很轻松,有很多知识运用的 不是很熟,但是基本的知识是能 ...