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. .net 超长URL请求返回404错误-解决方法

    <system.webServer> <security> <requestFiltering> <requestLimits maxQueryString= ...

  2. js对象原型prototype

    javascript中的每个对象都有prototype属性,Javascript中对象的prototype属性的解释是:返回对象类型原型的引用. 每一个构造函数都有一个属性叫做原型 1.1. 原型法设 ...

  3. Ehcart整合百度地图

    最近上班有些时间,学习了一下Ehcart的知识,自己制作了一份Ehcart整合百度地图的示例代码. GItHub地址:https://github.com/TianYanFd/tianjin-powe ...

  4. Neject 在MVC框架中使用

    Neject 开始是用3.3.0.0,不能自动生成NinjectWebCommon文件,测试了很久发现,是版本的问题 ,后来用Nuget卸载后,重新下了Ninject,Ninject.Web.Comm ...

  5. 团队第四次 # scrum meeting

    github 本此会议项目由PM召开,召开时间为4-8日晚上10点 召开时长15分钟 任务表格 袁勤 负责编写登陆逻辑 https://github.com/buaa-2016/phyweb/issu ...

  6. mysql 关联

    自关联 设计省信息的表结构provinces id ptitle 设计市信息的表结构cityscitys表的proid表示城市所属的省,对应着provinces表的id值 id ctitle proi ...

  7. java 连接SQL Server

    1.确认服务器的连通性,并且使用账户密码模式登陆有效. 1).登陆服务器 2).查看安全性 2.新建数据库用于测试 3.下载jdbc安装并配置 进入微软官网主页--> 搜索JDBC-->找 ...

  8. Exce 快捷键 tips

    1. 填充快捷键 ctrl+R  向下填充 CTRL+D 向右填充 2. 筛选快捷键 CTRL+SHIFT+L 3. 移动到当前区域的边缘: Ctrl + shift + 方向箭头 4. 字符连接:& ...

  9. java和.net连接数据库的方法

    .net连接数据库的方法 App.config <connectionStrings> <add name="" connectionString="D ...

  10. Windows服务器

    知道了怎么装VMware workstation并且创建虚拟机装上了系统配好网络