• Route Rendering Props

对于给定的路由如何渲染组件,有三种选项:component、render、children。你可以查看 <Route> 的文档来获取更多的信息,但是在这我们关注 component 和 render ,因为这两个是经常用到的。

component 应该被用于当你有一组件(有状态的组件或者是一个函数组件),并且想要渲染它的时候

render ,它采用内联函数,只有当你必须将范围内的变量传递给要渲染的组件时,才应该使用它。

你不应该使用带有内联函数的组件支持来传递范围内的变量,因为您将得到不需要的组件卸载/重新配置。

const Home = () => <div>Home</div>

const App = () => {
const someVariable = true; return (
<Switch>
{/* these are good */}
<Route exact path='/' component={Home} />
<Route
path='/about'
render={(props) => <About {...props} extra={someVariable} />}
/>
{/* do not do this */}
<Route
path='/contact'
component={(props) => <Contact {...props} extra={someVariable} />}
/>
</Switch>
)
}
  • Navigation

React Router 提供了一个<Link> 组件在你的应用程序中创建链接。无论在哪里渲染 <Link> ,一个锚点(<a>)将在应用程序的 HTML 中呈现

<Link to='/'>Home</Link>
// <a href='/'>Home</a>

<NavLink> 是 <Link> 的一个特殊类型,当其与当前位置匹配时它是活动的

// location = { pathname: '/react' }
<NavLink to='/react' activeClassName='hurray'>React</NavLink>
// <a href='/react' className='hurray'>React</a>

如果您想强制导航,您可以渲染一个<Redirect>。当一个<Redirect>呈现时,它将使用它来导航。

<Redirect to='/login'/>

先做个记录,挑个时间再来完善修改

React Router 4 的使用(2)的更多相关文章

  1. [Redux] Filtering Redux State with React Router Params

    We will learn how adding React Router shifts the balance of responsibilities, and how the components ...

  2. [转] React Router 使用教程

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

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

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

  5. React Router基础使用

    React是个技术栈,单单使用React很难构建复杂的Web应用程序,很多情况下我们需要引入其他相关的技术 React Router是React的路由库,保持相关页面部件与URL间的同步 下面就来简单 ...

  6. 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)

    请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...

  7. react router 4.0以上的路由应用

    thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr& ...

  8. React Router 使用教程

    一.基本用法 React Router 安装命令如下. $ npm install -S react-router 使用时,路由器Router就是React的一个组件. import { Router ...

  9. 关于react router 4 的小实践

    详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...

  10. React Router 4.x 开发,这些雷区我们都帮你踩过了

    前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...

随机推荐

  1. BNU 20950 ——沉重的货物 —————— · 最短路、最短边最大化」

    沉重的货物 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: ...

  2. jQuery 文本插入和标签移动方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. FontAwesome 图标 class="fa fa-home"

    本文转自:http://www.yeahzan.com/fa/facss.html 引用方式: class="fa fa-home" 注意:每个图标的引用都必须要添加 fa fa- ...

  4. python学习(六)--正则的一些例子

    import re #正则表达式#compile函数,--将正则表达式转变为内部函数,提高执行效率strr = "python123456"pattern = "Pyth ...

  5. 数组和矩阵(2)——Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  6. centOs升级

    因为军佬放弃制作Centos7的网络重装包,又Centos7的安装引导和6有较大区别所以,选择曲线救国(技术不行,只能这样乱搞)前文:Centos6.9一键重装包https://ppx.ink/net ...

  7. jQuery 添加样式属性的优先级别

    jQuery类中添加多个属性 $('#five .a') .css({ color:'blue', border:'2px solid green', background:'blue' }); jQ ...

  8. python闭包&装饰器&偏函数

    什么是闭包? 首先还得从基本概念说起,什么是闭包呢?来看下维基上的解释: 在计算机科学中,闭包(Closure)是词法闭包(Lexical Closure)的简称,是引用了自由变量的函数.这个被引用的 ...

  9. sqlserver broker远端端点证书认证

    1:采用windows验证的方法: CREATE ENDPOINT InstInitiatorEndpoint STATE = STARTED AS TCP ( LISTENER_PORT = ) F ...

  10. mysql 统计连续天数

    以下为例子数据 图1 图1 首先根据要求取出BeforeMeal要在7.0以下 并且 bingAfterMeal要在11.1以下 select AccountId,CreateTime from Di ...