React Router的Route的使用
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的使用的更多相关文章
- [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 ...
- [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 ...
- 【react router路由】<Router> <Siwtch> <Route>标签
博客 https://www.jianshu.com/p/ed5e56994f13?from=timeline 文档 http://react-guide.github.io/react-router ...
- [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 ...
- [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 ...
- [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 ...
- [转] React Router 使用教程
PS:react-route就是一个决定生成什么父子关系的组件,一般和layout结合起来,保证layout不行,内部的子html进行跳转 你会发现,它不是一个库,也不是一个框架,而是一个庞大的体系. ...
- [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 ...
- [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 ...
随机推荐
- activiti中的查询sql
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- 更新 TeX Live 软件包
这个 TeX Live 软件,你得时常更新一下,不然会遇到一些由软件包自身 Bug 导致的编译问题.比如,这次我使用 Beamer 软件包写演示文稿,就遇到问题了,结果发现是软件包自身存在的问题.安装 ...
- .NET版本与CLR版本及兼容性
WIN7原生的.net是2.0和3.5,打开和关闭windows功能里面可以看到.另外C:\Windows\Microsoft.NET\Framework下面也能看到..net framework1. ...
- [数据库]Sql server 数据库的备份和还原____还原数据库提示“介质集有2个介质簇,但只提供了1个。必须提供所有成员”
在对数据库备份与还原的过程中,我遇到一个问题“介质集有2个介质簇,但只提供了1个.必须提供所有成员”,下面详细的介绍一下遇到问题的经过与问题解决的方法! 一.备份与还原遇到的问题描述与解决方法: 前两 ...
- 微信小程序是怎么运行的?
微信客户端在打开小程序之前,会把整个小程序的代码包下载到本地. 紧接着通过 app.json 的 pages 字段就可以知道你当前小程序的所有页面路径 而写在 pages 字段的第一个页面就是这个小程 ...
- 跨年呈献:HP-Socket for Linux 1.0 震撼发布
三年,三年,又三年,终于,终于,终于不用再等啦!就在今天,HP-Socket for Linux v1.0 震撼发布!还是一样的接口,一样的高效,一样的简便,一样的味道. HP-Socket ...
- 让WebStrom支持SSH协议的子项目
让WebStrom支持SSH协议的子项目 在大项目中, 经常会遇到子项目(submodule)使用ssh的情形, 但是WebStrom不直接支持它. 下面以MAC为例,在PC中的处理类似. 打开ter ...
- 初识Git->GitHub
这不是一篇教程,这是学习过程的一个记录,初次使用GitHub的小白请移步文章末尾的参考链接, 先了解Git是什么东西 Git的工作流程 操作Git 练习使用 边用边学 #--------------- ...
- springcloud相关资料收集
http://springboot.fun/ Spring Boot 中文索引 http://springcloud.fun/ Spring Cloud 中文索引 https://spring ...
- Exp5 MSF基础应用 20164313 杜桂鑫
1. 实践目标 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.1一个主动攻击实践,如ms08_067; (1分) 1.2 一个针对浏览器的攻击, ...