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.

转载于:https://www.cnblogs.com/YangBinChina/p/10164787.html

React Router的Route的使用相关推荐

  1. React router 的 Route 中 component 和 render 属性理解

    React router 的 Route 中 component 和 render 属性理解 Route 标签的三个互斥属性 render.component.children Route 就是用来匹 ...

  2. router路由react_使用React Router在React中受保护的路由

    router路由react In this video, you will see how to create a protected route using React Router. This r ...

  3. React Router教程–如何使用代码示例渲染,重定向,切换,链接等

    If you have just started with React, you are probably still wrapping your head around the whole Sing ...

  4. 关于React Router v4的虚张声势指南

    In this article, we'll cover the important things you can quickly learn to be informed and confident ...

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

  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. hitchhiker部署_Hitchhiker的React Router v4指南:无限远的递归路径!

    hitchhiker部署 Welcome to the third part of the Hitchhiker's Guide to React Router v4. In this article ...

  8. 使用React Router以编程方式导航

    通过react-router我可以使用Link元素来创建由react路由器本地处理的链接. 我在内部看到它调用this.context.transitionTo(...) . 我想从下拉列表中进行导航 ...

  9. 从 React Router 谈谈路由的那些事

    React Router 是专为 React 设计的路由解决方案,在使用 React 来开发 SPA (单页应用)项目时,都会需要路由功能,而 React Router 应该是目前使用率最高的. Re ...

  10. React Router 黑笔记?

    我横竖睡不着,字里行间看出2个字.... 首先,在该项目开始之前,还请大家能够先下载一个项目脚手架.本教程基于该脚手架进行开发 先看文件大致架构 渲染 Route (index.js 启动的compo ...

最新文章

  1. 报错引发的版本对应——tensorflow+keras+python版本对应(全)
  2. Ubuntu 下一个 vim 建立python 周围环境 构造
  3. MySQL-01:下载安装配置及初始化命令
  4. 未来计算机将具有图像识别 定理证明,[单选] 低温计与高温计所测温度的分界线为()。...
  5. java设置行显示复选框_java spring cheakbox复选框怎么行或者列删除添加
  6. 如何在 Mac 上安排电子邮件?
  7. windows核心编程第二章阅读
  8. 4399小游戏怎样下载到本地玩
  9. 我的阿里巴巴图标公开库
  10. [转载] 七龙珠第一部——第111话 龟仙人最后的魔封波
  11. 2022年中国手机市场销量vivo第1,小米滑落至第5
  12. CSS技巧系列--使用视频作为背景
  13. 盐城北大青鸟东台基地开展春季游学活动
  14. [electron] 基础学习
  15. 日记侠:微信最简单最轻松的赚钱模式
  16. php中网页字体颜色的代码,HTML5画布如何设置字体颜色?(代码示例)
  17. PHPOK 5.2.009 发布
  18. 计算机汇编语言教程pdf,计算机汇编语言入门.pdf
  19. 记事本写HTML中文出现乱码的问题
  20. 等差数列和等比数列的通式和求和、求积公式

热门文章

  1. modbus调试工具 linux,Modbus测试工具 :Modbus Poll,Modbus Slave
  2. 五、Oracle学习笔记:DQL数据查询语句
  3. 阿里云ddns解决动态IP问题
  4. Centos7 Kubernetes(k8s) 开发服务器(单服务器)部署 prometheus 监控
  5. JAVA Maven和ANT 安装 Linux(Ubuntu/Centos),Mac
  6. PrintStream 打印流、System 剖析、Java 16 进制转换
  7. ES6 面向对象编程
  8. Html加水印和禁用复制和右键(jquery.watermark.js)
  9. 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第6节 Lambda表达式_7_Lambda表达式有参数有返回值的练习...
  10. HttpClient读取数据乱码的解决方案