概要

开发单页应用, 首先绕不开的内容就是路由, react router v4 版本是最新的版本. 和之前的版本相比, 成熟了很多, 也简单了很多, 使用起来更加方便.

核心 component

react-router V4 可以用于 Web 和 Native. 这里主要讨论基于 Web 的应用.

react-router 有很多 Components, 但是只要掌握下面 3 个 Component 就可以管理好 react 应用的路由了.

Router component

Router 是整个应用的路由. 在 Web 应用中, 使用 BrowerRouter 来包装 App

<BrowserRouter>
<App />
</BrowserRouter>

然后, 在 App component 中, 就可以使用 Route 来定义各个路由对应的 component

Route component

每个 Route 都是一个具体的路由, 对应一个具体的页面. Route 可以对应一个 Component, 也可以直接写个匿名的 render 函数.

Route 中最常用的属性就是 path, 也就是路由的地址, 除此之外, Route 最重要的作用是会将 { match, location, history } 3 个属性注入到对应的 Component 或者 render 函数中.

Link component

Link 是用来导航的, 也就是在不同 Route 之间切换就会用到 Link.

常用路由示例

示例工程

示例工程的创建采用 create-react-app 工具.

$ create-react-app route-test
$ cd route-test
$ yarn add react-router-dom

基本使用

修改工程中的 App.js 文件, 增加 route 的 sample

import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
import './App.css' const Index = () => <h2>Home</h2>
const About = () => <h2>About</h2>
const Users = () => <h2>Users</h2> class App extends Component {
render() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about/">About</Link>
</li>
<li>
<Link to="/users/">Users</Link>
</li>
</ul>
</nav> <Route path="/" exact component={Index} />
<Route path="/about/" component={About} />
<Route path="/users/" component={Users} />
</div>
</Router>
)
}
} export default App

这个示例中, 就包含了 3 个常用组件 Router, Route 和 Link

路由参数

路由的参数可以加在 path 上, 下面的示例中, Users Component 可以通过注入的 match 来得到 URL 中的参数.

import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
import './App.css' const Index = () => <h2>Home</h2>
const About = () => <h2>About</h2>
const Users = ({ match }) => (
<h2>
User is {match.params.name}, age: {match.params.age}
</h2>
) class App extends Component {
render() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about/">About</Link>
</li>
<li>
<Link to="/users/test/16">Users</Link>
</li>
</ul>
</nav> <Route path="/" exact component={Index} />
<Route path="/about/" component={About} />
<Route path="/users/:name/:age" component={Users} />
</div>
</Router>
)
}
} export default App

query 参数

除了上面那种 RESTful 的参数方式, 也可以通过 query 来传递参数. 下例中, 通过 location.search 来获取 querystring, 至于解析 querystring, 已有很多现成的方法可以使用.

import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Link } from 'react-router-dom'
import './App.css' const Index = () => <h2>Home</h2>
const About = ({ match, location }) => (
<h2>About's search string: {location.search}</h2>
)
const Users = ({ match }) => (
<h2>
User is {match.params.name}, age: {match.params.age}
</h2>
) class App extends Component {
render() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to={{ pathname: '/about/', search: '?name=test&age=16' }}>
About
</Link>
</li>
<li>
<Link to="/users/test/16">Users</Link>
</li>
</ul>
</nav> <Route path="/" exact component={Index} />
<Route path="/about/" component={About} />
<Route path="/users/:name/:age" component={Users} />
</div>
</Router>
)
}
} export default App

FAQ

在刚开始使用 react-router 时, 可能会有些疑问, 我整理了自己在使用中一些疑惑:

match 是如何注入到 Component 中的

match, location, history 这 3 个 object, 是由 Route Component 作为 props 注入到它的 render 方法, 或者 component={xxx} prop 对应的组件中的

match.path 和 match.url 有什么区别

  • match.path 是用来做匹配用的, 也就是在 Route 中使用的
  • match.url 是用来迁移页面用的, 也就是在 Link 中使用的

比如上面的例子中, 对于 /users

  • match.path 是 users:name/:age
  • match.url 是 /users/test/16

BrowserRouter 和 HashRouter 有什么区别

  • BrowserRouter 是用于和 Server 有交互的 Router
  • HashRouter 是用于静态文件服务的 Router

Route 和 Switch 的区别

当有多个 Route 并列时, 如果外层没有包裹 Switch, 那么匹配到的 Route 都会被 render, 如果有 Switch, 那么只会 render 第一个匹配上的 Route

Route 如果不设置 path, 则每次都会匹配成功

Link 和 NavLink 的区别

NavLink 是一种特殊的 Link, 它可以设置一些 active 时的样式.

路由信息和 redux 状态管理如何结合

路由信息本质上也是一种状态信息, 放不放在 redux 中来管理, 相信很多人都会纠结. 官方的建议是不要把路由的状态放在 redux 的 store 中来管理, 理由如下:

  1. 路由状态一般只有 Components 才关心, 不管放不放在 redux 中, Component 的代码不会有什么改变
  2. 大部分情况下, 路由跳转都是通过 Link, NavLink, Redirct 来完成, 如果需要通过代码, 或者异步 action 中来跳转, 可以通过传入 history 对象来完成.
  3. 路由变化一般不需要通过时间旅行(time travel, redux 的一个调试工具)来 debug.

react-router(v4)的更多相关文章

  1. [Web 前端] React Router v4 入坑指南

    cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...

  2. React Router V4发布

    React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...

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

  4. [React Router v4] Redirect to Another Page

    Overriding a browser's current location without breaking the back button or causing an infinite redi ...

  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 v4] Render Catch-All Routes with the Switch Component

    There are many cases where we will need a catch-all route in our web applications. This can include ...

  8. [React Router v4] Render Nested Routes

    With React Router v4 the entire library is built as a series of React components. That means that cr ...

  9. [React Router v4] Parse Query Parameters

    React Router v4 ignores query parameters entirely. That means that it is up to you to parse them so ...

  10. [React Router v4] Use Regular Expressions with Routes

    We can use regular expressions to more precisely define the paths to our routes in React Router v4. ...

随机推荐

  1. Android之CircleImageView使用

    文章大纲 一.什么是CircleImageView二.代码实战三.项目源码下载 一.什么是CircleImageView   圆角 ImageView,在我们的 App 中这个想必是太常见了,也许我们 ...

  2. DNS服务详解

    DNS服务 目录: 一.DNS原理 二.DNS服务的安装与配置 三.DNS信息收集 一.DNS原理 1.hosts文件与DNS服务器 1.1hosts文件 目录:C:\WINDOWS\system32 ...

  3. 阿里巴巴excel工具easyexcel 助你快速简单避免OOM

    Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有 ...

  4. 【RL-TCPnet网络教程】第39章 RL-TCPnet之TFTP服务器

    第39章     RL-TCPnet之TFTP服务器 本章节为大家讲解RL-TCPnet的TFTP服务器应用,学习本章节前,务必要优先学习第38章的TFTP基础知识.有了这些基础知识之后,再搞本章节会 ...

  5. 网卡的 Ring Buffer 详解

    1. 网卡处理数据包流程 网卡处理网络数据流程图: 图片来自参考链接1 上图中虚线步骤的解释: DMA 将 NIC 接收的数据包逐个写入 sk_buff ,一个数据包可能占用多个 sk_buff , ...

  6. Java数据结构和算法 - 高级排序

    希尔排序 Q: 什么是希尔排序? A: 希尔排序因计算机科学家Donald L.Shell而得名,他在1959年发现了希尔排序算法. A: 希尔排序基于插入排序,但是增加了一个新的特性,大大地提高了插 ...

  7. python --- 基数排序算法

    基数排序的方式可以采用LSD,由键值的最右边开始,适用于数值整数.或者MSD,由键值的最左边开始,适用于字符串整数.在LSD基数排序中每一次的处理都是将关键字按顺序放置在其各自的称为桶的数据结构中,而 ...

  8. python进程和线程(四)

    线程同步条件.信号量及队列 同步条件(event) 下面是官方文档对event的一些解释: An event is a simple synchronization object; the event ...

  9. Android-PickerView【仿iOS的PickerView控件,并封装了时间选择和选项选择这两种选择器】使用

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 本文主要演示Android-PickerView的选项选择器.时间选择器的简单运用.由于每一个版本略有不用,所以实际使用方式以git ...

  10. .net core自定义高性能的Web API服务网关

    网关对于服务起到一个统一控制处理的作用,也便于客户端更好的调用:通过网关可以灵活地控制服务应用接口负载,故障迁移,安全控制,监控跟踪和日志处理等.由于网关在性能和可靠性上都要求非常严格,所以针对业务需 ...