react-router(v4)
概要
开发单页应用, 首先绕不开的内容就是路由, 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 中来管理, 理由如下:
- 路由状态一般只有 Components 才关心, 不管放不放在 redux 中, Component 的代码不会有什么改变
- 大部分情况下, 路由跳转都是通过 Link, NavLink, Redirct 来完成, 如果需要通过代码, 或者异步 action 中来跳转, 可以通过传入 history 对象来完成.
- 路由变化一般不需要通过时间旅行(time travel, redux 的一个调试工具)来 debug.
react-router(v4)的更多相关文章
- [Web 前端] React Router v4 入坑指南
cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
- React Router V4发布
React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...
- [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] Redirect to Another Page
Overriding a browser's current location without breaking the back button or causing an infinite redi ...
- [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 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 ...
- [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 ...
- [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 ...
- [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. ...
随机推荐
- RationalRose 安装过程中无法加载镜像的问题
前情提要:本文主要以提供关键问题的解决思路为目的,境况紧急的,在核对好所遇问题与博主是否一致后,可以直接跳到最后看解决办法即可. 另外,本文重要部分采用不同色文字,加以强调. 任务:安装Rationa ...
- 2018-08-20 中文代码之Spring Boot集成H2内存数据库
续前文: 中文代码之Spring Boot添加基本日志, 源码库地址相同. 鉴于此项目中的数据总量不大(即使万条词条也在1MB之内), 当前选择轻量级而且配置简单易于部署的H2内存数据库比较合理. 此 ...
- SQLServer多表联查,多表分页查询
多表联查: select p.*,s.Sheng , i.Shifrom [dbo].[ProductRecordInfo] --表名 p left join [ShengInfo] s on ...
- pyquery 学习
pyquery 是python仿照jQuery的严格实现,语法与jQuery几乎完全相同,所以对于学过前端的朋友们可以立马上手,没学过的小朋友也别灰心,我们马上就能了解到pyquery的强大. 1 安 ...
- Fiddler-弱网测试设置
第一步:打开模拟弱网环境 第二步:打开配置文件 第三步:修改配置参数 m_SimulateModem,修改后最好 Ctrl+S 保存一下 第四步:修改好参数返回后需要再次打开弱网环境 以上弱网设置就 ...
- VS2013 百度云资源以及密钥
https://pan.baidu.com/s/1eu3XycWO8fWItmkFeYNv9w提取码:dy9r 密钥:BWG7X-J98B3-W34RT-33B3R-JVYW9 vs2015 http ...
- “挑三拣四”地学一学Java I/O
古人云:“读书破万卷,下笔如有神”.也就是说,只有大量的阅读,写作的时候才能风生水起——写作意味着输出(我的知识传播给他人),而读书意味着输入(从他人的知识中汲取营养). 对于Java I/O来说,I ...
- TabBottomFragmentLayout【自定义底部选项卡区域(搭配Fragment)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 自定义底部选项卡布局LinearLayout类,然后配合Fragment,实现切换Fragment功能. 缺点: 1.底部选项卡区域 ...
- 玩转Spring Cloud之配置中心(config server &config client)
本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1.2.svn方式 1.3.本地文件方式 1.4.解决配置中包含中文内容返回乱码问题 二.搭建配置消费客户端( ...
- 通用mapper认识和用法
目录 0. 认识 1. 导包 2. mybatis的config文件:mybatis-mapper-config.xml 3. spring与mybatis整合配置文件:mybatis.xml 4. ...