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 ...
随机推荐
- 发布一个PHP包到Packagist, 然后使用Composer安装
Composer 能够方便的进行项目的依赖管理, 当我们发布一个包并且希望别人通过Composer安装的时候, 就需要将包发布到Composer的包仓库Packagist上面. 下面进行详细的说明一 ...
- Promise.all处理多个异步请求
一个前台页面需要请求2个rest接口获取数据,一个用于解析文件获取列名,一个查询数据库获得列值. 有很低的概率页面显示为空,刷新可能就有显示了. 使用Promise.all就解决了上面的问题,2部分数 ...
- 图解Tomcat
- windows 下借助7zip实现命令行解压缩
windows 下借助7zip实现命令行解压缩 64位电脑下载 https://www.7-zip.org/a/7z1805-x64.exe 安装 安装目录下所有文件如下: 在命令行下只需要用到 7z ...
- CBSN NEWS
https://www.cbsnews.com/video/fatal-crossing/
- Halcon Visinpro 破解版
目前测试过的破解版资料: halcon10 可用已测 完美破解 halcon12 可用已测 完美破解 halcon13 可用已测 完美破解 halcon17 可用已测 ...
- 使用Linux的环境变量
许多程序和脚本都使用环境变量来获取系统信息,并存储临时数据和配置信息: 1.什么是环境变量 用来存储关于shell会话和工作环境的信息,就叫做环境变量: bash shell下两种类型: 1.全局变量 ...
- 对struts2一些理解
1.strust2框架是什么?为解决什么问题出现? Struts2是在WebWork+xwork基础发展而来的. 2. strust2的优缺点优点: 支持Ajax 支持Ognl标签 提供了强大的拦截器 ...
- 缓存--Redis
Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- Django基础模型层(77-78)
jango框架之模型层(d77-78)一 单表操作: 1 mysql数据库:settings里配置 'default': { # key值必须都是大写 'ENGINE': 'django.d ...