概述

react-router V4 相对于react-router V2 or V3 几乎是重写了, 新版的react-router更偏向于组件化(everything is component)。

V4汲取了很多思想,路由即是组件,使路由更具声明式,且方便组合。如果你习惯使用react,那么一定会很快上手新版的react-router

react-router V4 被一分为三: react-router-dom(for web)、react-router-native(for native)、react-router(core)。但如果仅在浏览器中使用的话,一般只需要用到react-router-dom就可以了。

改动点

1. Router/Route 的改变

// V2 or V3
import { Router, Route, hashHistory } from 'react-router'; <Router history={hashHistory}>
<Route path='/foo' component={Foo} />
<Route path='/bar' component={Bar} />
</Router>
// V4 Router组件里只能渲染一个组件
import {
HashRouter as Router,
Route
} from 'react-router-dom'; <Router>
<div>
<Route path='/foo' component={Foo} />
<Route path='/bar' component={Bar} />
</div>
</Router>

2. 组件嵌套

// V2 or V3 路由组件嵌套
import { Router, Route, hashHistory } from 'react-router'; <Router history={hashHistory}>
<Route path='/' component={App}>
<Route path='foo' component={Foo} />
<Route path='bar' component={Bar} />
</Route>
</Router>
// V4 Router 的路由组件嵌套
import {
HashRouter as Router,
Route,
Switch
} from 'react-router-dom'; <Router>
<Route path="/" component={(props) => (
<App {...props}>
<Switch>
<Route path='/foo' component={Foo} />
<Route path='/bar' component={Bar} />
</Switch>
</App>
)}/>
</Router>

3. 路由的生命周期

react-router V4中去掉了on****的路由生命周期的钩子,但是你可以在组件中用componentDidMount 或 componentWillMount代替 onEnter,可以用componentWillUpdate 或 componentWillReceiveProps代替 onUpdate,你可以用componentWillUnmount代替 onLeave

4. Link

// V2 or V3
import { Link } from 'react-router'; // V4
import { Link } from 'react-router-dom';

5. history.push and history.replace

// V2 or V3
history.push({
pathname: '/home',
query: {
foo: 'test',
bar: 'temp'
}
});
history.replace({
pathname: '/home',
query: {
foo: 'test',
bar: 'temp'
}
}); // V4
history.push({
pathname: '/home',
search: '?foo=test&bar=temp',
});
history.replace({
pathname: '/home',
search: '?foo=test&bar=temp',
});

6. props.params

// V2 or V3 获取params可以这么获取
this.props.params // V4
this.props.match.params

7. location.query

// V2 or V3 获取query可以这么获取
this.props.location.query // V4 去掉了location.query,只能使用search来获取,为了让其跟浏览器一样
// 如果想要兼容以前的location.query,可以使用query-string库解析一下
// 如: queryString.parse(props.location.search)
this.props.location.search

8. location.action

// V2 or V3 获取location的action
this.props.location.action // V4 去掉了location.action, 放在了history里面
history.action

9.关于history

以前获取react-router里面的history库,可以这么获取:

import {hashHistory as history} from 'react-router';

react-router V4:

import createHashHistory as history from 'history/createHashHistory';

兼容处理

因为要从react-router V2完全迁移到react-router V4工作量还是挺大的,一下子难以完全迁移,所以对某些地方做了兼容处理。

history

import _ from 'lodash';
import queryString from 'query-string'; function processHistory(history) {
const _push = history.push;
const _replace = history.replace; history.push = function (one) {
if (!_.isPlainObject(one)) {
return _push.apply(this, arguments);
}
const o = Object.assign({}, one);
if (o.query) {
o.search = queryString.stringify(o.query);
}
_push.apply(this, [o]);
}; history.replace = function (one) {
if (!_.isPlainObject(one)) {
return _replace.apply(this, arguments);
}
const o = Object.assign({}, one);
if (o.query) {
o.search = queryString.stringify(o.query);
}
_replace.apply(this, [o]);
}; return history;
} export default processHistory;

props

import queryString from 'query-string';

const processReactRouterProps = (props) => {
const newProps = Object.assign({}, props);
newProps.location.query = queryString.parse(props.location.search);
newProps.location.action = newProps.history.action;
newProps.params = props.match.params || {}; // 不止 || 是否有意义
return newProps;
}
export default processReactRouterProps;

参考资料:

ReactRouter升级 v2 to v4的更多相关文章

  1. ubuntn 内核升级到LINUX v4.11.8:

    升级到LINUX v4.11.8: http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.11.8/linux-headers-4.11.8-041108_ ...

  2. react-router 从 v2/v3 to v4 迁移(翻译)

    react-router v4 是完全重写的,所以没有简单的迁移方式,这份指南将为您提供一些步骤,以帮助您了解如何升级应用程序. 注意: 这份迁移指南适用于react-router v2和v3,但为简 ...

  3. GoogLeNet 之 Inception v1 v2 v3 v4

    论文地址 Inception V1 :Going Deeper with Convolutions Inception-v2 :Batch Normalization: Accelerating De ...

  4. 从Inception v1,v2,v3,v4,RexNeXt到Xception再到MobileNets,ShuffleNet,MobileNetV2

    from:https://blog.csdn.net/qq_14845119/article/details/73648100 Inception v1的网络,主要提出了Inceptionmodule ...

  5. 51nod Bash游戏(V1,V2,V3,V4(斐波那契博弈))

    Bash游戏V1 有一堆石子共同拥有N个. A B两个人轮流拿.A先拿.每次最少拿1颗.最多拿K颗.拿到最后1颗石子的人获胜.如果A B都很聪明,拿石子的过程中不会出现失误.给出N和K,问最后谁能赢得 ...

  6. 魔方NewLife.Cube升级v2.0

    魔方是一套集成权限管理的MVC管理后台,最具特色功能是模版覆盖机制,是XCode实体类的最佳搭档! v2.0.2017.1126   借助Ajax支持高级操作,如:删除选中.批量启用禁用等 用户管理增 ...

  7. 信驰达蓝牙4.0模块全面升级 v2.20 U最新发布

    作为国际蓝牙联盟成员之一,德州仪器(TI)于2012年强势推出CC254X系列单芯片(SOC)低功耗蓝牙收发器,经典51内核,最强优势在于丰富的外围(21个IO,UART,SPI,USB2.0,PWM ...

  8. td-agent的v2,v3,v4版本区别

    官方地址:https://docs.fluentd.org/quickstart/td-agent-v2-vs-v3-vs-v4

  9. react-router v4 路由规则解析

    前言 react-router升级到4之后,跟前面版本比有了很大的差别. 例如包的拆分,动态路由等详细的差别就不说了,各位大神的总结也很到位,详细可以点击看看,All About React Rout ...

随机推荐

  1. RN错误随笔 - Unable to resolve module 'AccessibilityInfo'

    错误信息:.React Native 运行报错:Unable to resolve module 'AccessibilityInfo' 可以看到在 异常的返回的JSON 结构中给出了推荐的解决方法 ...

  2. linux shell基本知识

    shell script的一些注意事项: .#这个符号是注释本行,通常用来做批注用,#!除外,是用来标注用哪种shell执行本脚本, .执行顺序为从上到下,从做到右 .忽略空行,tab空格 .脚本换行 ...

  3. Flink的容错

    checkpoint介绍 checkpoint机制是Flink可靠性的基石,可以保证Flink集群在某个算子因为某些原因(如 异常退出)出现故障时,能够将整个应用流图的状态恢复到故障之前的某一状态,保 ...

  4. Codeforces 305E Playing with String 博弈

    我们可以把每段连续可以选的字符看成一个游戏, 那么sg[ i ]表示连续 i 个字符可选的sg值. 然后找找第一个就好啦. #include<bits/stdc++.h> #define ...

  5. 分布式配置hadoop2.5.0 2.6.x

    1. sudo vim /etc/hostname 在master的机器上,改成     master 在slave上写  slave01,02,03...... 配置好后重启. 2. sudo vi ...

  6. UOJ#207. 共价大爷游长沙 LCT

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ207.html 题解 第一次听说 LCT 还可以维护子树信息. 首先对于每一条路径 rand 一个值,分别 ...

  7. day64 django django零碎知识点整理

    本文转载自紫金葫芦,哪吒,liwenzhou.cnblog博客地址 简单了解mvc框架和MTV框架, mvc是一种简单的软件架构模式: m----model,模型 v---view,视图 c---co ...

  8. Springboot 1.5.x 集成基于Centos7的RabbitMQ集群安装及配置

    RabbitMQ简介 RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件). RabbitMQ是一套开源(MPL)的消息队列服务软件,是由LShift提供的一 ...

  9. react编码规范

    1.每个文件只写一个组件,但是多个无状态组件可以放在单个文件中: 2.有内部状态,方法或要对外暴露ref的组件,用类式组件: 3.无内部状态,方法或无需对外暴露ref的组件,用函数式组件: 4.有内部 ...

  10. winfrom datagridview中DataGridViewTextBoxColumn的联动处理

    这个问题有两种方法 第一种是用DataGridview中自带的DataGridViewTextBoxColumn 控件,第二种是动态添加combobox控件 方法一: 首先 窗体上拖拽一个 DataG ...