ReactRouter升级 v2 to v4
来源:互联网 发布:星空卫视直播软件下载 编辑:程序博客网 时间:2024/06/03 16:01
概述
react-router V4 * 相对于 *react-router V2 or V3 几乎是重写了, 新版的react-router更偏向于组件化(everything is component)。
V4
汲取了很多思想,路由即是组件,使路由更具声明式,且方便组合。如果你习惯使用 react
,那么一定会很快上手新版的 react-router
。http://www.tuicool.com/articles/uIveAfI
react-router V4 被一分为三: react-router-dom
(for web)、 react-router-native
(for native)、 react-router
(core)。但如果仅在浏览器中使用的话,一般只需要用到 react-router-dom
就可以了。
改动点
1. Router/Route 的改变
// V2 or V3import { 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 V3import { Link } from 'react-router';// V4import { Link } from 'react-router-dom';
5. history.push and history.replace
// V2 or V3history.push({ pathname: '/home', query: { foo: 'test',bar: 'temp' }});history.replace({ pathname: '/home', query: { foo: 'test',bar: 'temp' }});// V4history.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// V4this.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的actionthis.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;
参考资料:
react-router2 迁移到 react-router4 关注点
react-router 官方文档
- ReactRouter升级 v2 to v4
- ReactRouter升级 v2 to v4
- ReactRouter升级 v2 to v4
- react-router 从 v2/v3 to v4 迁移
- V4升级V5解决方案
- 升级ssl v2
- Sandboxie v4.14 - v4.xx Power Activator v2.0 Ultimate
- 【Trufun升级公告】升级版V2.3.5
- ReactRouter 4 前瞻
- 此次V4升级的经验教训
- V4升级V5pdf,flash打印的问题
- 升级ubuntu 16.10内核到v4.13.7
- Sika.CarboDur.v4.2 1CD+Modelist.v2.2
- Geocentrix.ReWaRD.v2.5.21.903 挡土墙设计+Strat.V4.5
- 【Tensorflow slim 实战】写Inception-V4 Inception-ResNet-v2结构
- Inception v1 —v2 —v3 —v4
- ReactRouter 离开页面的钩子
- 中游升级助手 v2.62 怎么用
- MyBatis一级缓存的简单剖析
- Android中如何使按钮的背景变得透明
- 如何从CSDN上转载文章
- .NET线程同步之SpinLock构造
- Date and Time in C++
- ReactRouter升级 v2 to v4
- centos7 mysql数据库安装和配置
- BZOJ 1131 Sta
- C++可变参数列表处理宏va_list、va_start、va_end的使用
- POJ 3017 Cut the Sequence 笔记
- 阿里云ESC搭建wampserver后外网访问不到问题
- Git的工作原理
- 蓝桥杯 ADV-102 算法提高 单词个数统计
- C语言-深度理解