react-router 从 v2/v3 to v4 迁移(翻译)
react-router v4 是完全重写的,所以没有简单的迁移方式,这份指南将为您提供一些步骤,以帮助您了解如何升级应用程序。
注意: 这份迁移指南适用于react-router v2和v3,但为简洁起见,对以前版本的引用仅提及v3。
The Router
在react-router v3中,仅有一个<Router> 组件,需要提供 history 对象作为他的属性 (prop)。
此外,您可以使用 routes 作为 <Router> 的属性 (prop) 或者作为 children 的方式来定义程序的路由结构。
// v3
import routes from './routes'
<Router history={browserHistory} routes={routes} />
// or
<Router history={browserHistory}>
<Route path='/' component={App}>
// ...
</Route>
</Router>
使用 react-router v4,一个最大的改变就是可以有很多不同的 router 组件,每个 router 组件都会为您创造出一个 history 对象,<BrowserRouter> 会创建 brower history,<HashRouter> 会创建 hash history,<MemoryRouter> 会创建 memory history。
在v4中,没有集中的路由配置。任何需要根据路由渲染内容的地方,您只需渲染一个 <Route> 组件。
// v4
<BrowserRouter>
<div>
<Route path='/about' component={About} />
<Route path='/contact' component={Contact} />
</div>
</BrowserRouter>
有一点需要注意的就是 router 组件只能被赋予一个子元素
// yes
<BrowserRouter>
<div>
<Route path='/about' component={About} />
<Route path='/contact' component={Contact} />
</div>
</BrowserRouter>
// no
<BrowserRouter>
<Route path='/about' component={About} />
<Route path='/contact' component={Contact} />
</BrowserRouter>
Routes
在 v3,<Route> 并不是一个组件,相反,您程序中所有的<Route> 元素仅用于创建路由配置对象。
/// in v3 the element
<Route path='contact' component={Contact} />
// 相当于
{
path: 'contact',
component: Contact
}
使用 v4,您可以像常规的 React 程序一样布局您应用中的组件,您要根据位置(特别是其 pathname )呈现内容的任何位置,您将呈现 <Route>
在 v4,<Route> 其实是一个组件,所以无论你在哪里渲染 <Route>,它里面的内容都会被渲染。当 <Route> 的 path 与当前的路径匹配时,它将会渲染 component, render, or children 属性中的内容,当 <Route> 的 path 与当前的路径不匹配时,将会渲染 null
路由嵌套
在 v3 中,<Route> 组件是作为其父 <Route> 组件的 children 嵌套其中的。
<Route path='parent' component={Parent}>
<Route path='child' component={Child} />
<Route path='other' component={Other} />
</Route>
当嵌套的 <Route> 匹配时,react 元素将会使用子 <Route> 和 父 <Route> 的 component 属性去构建,子元素将作为父元素的 children 属性。
<Parent {...routeProps}>
<Child {...routeProps} />
</Parent>
使用 v4,子 <Route> 应该由父 <Route> 呈现。
<Route path='parent' component={Parent} />
const Parent = () => (
<div>
<Route path='child' component={Child} />
<Route path='other' component={Other} />
</div>
)
on* 属性
react-router v3 提供 onEnter, onUpdate, and onLeave 方法。这些方法本质上是重写(覆盖)了 react 生命周期方法
使用 v4,你将会使用生命周期方法 通过 <Route> 渲染的组件,你可以使用 componentDidMount 或 componentWillMount 代替 onEnter,你可以使用 componentDidUpdate 或者 componentWillUpdate (更或者 componentWillReceiveProps) 代替 onUpdate,你可以使用 componentWillUnmount 代替 onLeave。
<Switch>
在v3中,您可以指定一些子路由,并且只会渲染匹配到的第一个。
// v3
<Route path='/' component={App}>
<IndexRoute component={Home} />
<Route path='about' component={About} />
<Route path='contact' component={Contact} />
</Route>
v4 通过 <Switch> 组件提供了相似的功能,当 <Switch> 被渲染时,它仅会渲染与当前路径匹配的第一个子 <Route>。
// v4
const App = () => (
<Switch>
<Route exact path='/' component={Home} />
<Route path='/about' component={About} />
<Route path='/contact' component={Contact} />
</Switch>
)
react-router 从 v2/v3 to v4 迁移(翻译)的更多相关文章
- [Web 前端] 你不知道的 React Router 4
cp from https://segmentfault.com/a/1190000010718620 几个月前,React Router 4 发布,我能清晰地感觉到来自 Twitter 大家对新版本 ...
- [Web 前端] React Router v4 入坑指南
cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
- React Router V4.0学习笔记
最近在学习React Router,但是网站的教程多半还是3.X版本之前的,所以我只能在GitHub上找到React Router的官方文档在读.后来总结了一下,包括学习经验以及V3.X与V4.X的差 ...
- 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 ...
随机推荐
- mysql主从复制跳过复制错误【转】
跳过复制错误 mysql因为binlog机制问题,有些时候会出现从库重放sql执行失败的情况,特别是旧的STATEMENT模式最容易出现这种情况(因为函数和存储过程等原因),这也是为什么强调使用mix ...
- 【译】ASP.NET Identity Core 从零开始
原文出自Rui Figueiredo的博客,原文链接<ASP.NET Identity Core From Scratch> 译者注:这篇博文发布时正值Asp.Net Core 1.1 时 ...
- 一个无锁消息队列引发的血案(六)——RingQueue(中) 休眠的艺术 [续]
目录 (一)起因 (二)混合自旋锁 (三)q3.h 与 RingBuffer (四)RingQueue(上) 自旋锁 (五)RingQueue(中) 休眠的艺术 (六)RingQueue(中) 休眠的 ...
- GeoHash解析及java实现
GeoHash解析请参考这里: http://www.open-open.com/lib/view/open1417940079964.html java实现GeoHash,代码已注释. import ...
- Java Map 接口
Map接口中键和值一一映射. 可以通过键来获取值. 给定一个键和一个值,你可以将该值存储在一个Map对象. 之后,你可以通过键来访问对应的值. 当访问的值不存在的时候,方法就会抛出一个NoSuchEl ...
- 我的CSS命名规则
常见class关键词: 布局类:header, footer, container, main, content, aside, page, section 包裹类:wrap, inner 区块类:r ...
- java代码分页
分页类 这个适用情况: 适用于前端页面已提供分页按钮样式的情况 分页规则: 首页,尾页,上页,下页 这四个按钮必定出现,中间分页动态生成5个 如:首 上 2 3 4 5 6 下 尾 public cl ...
- Hanoi Factorys
题面 思路 这道题看似难的一匹,实际上也难的一批还好,甚至n^2 DP都有50分呢. 原谅我一失手成千古恨. 50分思路 就是sort后根据条件DP if (LIS[i].b>LIS[j].a) ...
- iOS企业包安装注意事项详解(解决提示iPhone未受信任的问题)
请在Safari浏览器上打开该页面才能正常安装. 如果您是微信中查看该页面,请点击右上角,在弹出的菜单中选择“在Safari中打开” 对于iOS版本为7.X和8.X的用户,直接点击安装即可(此处为it ...
- 微信小程序开发之IOS/Android兼容坑(持续更新)
一.时间转换问题: 这不只是小程序上面的问题是ios系统 都有这个问题就是new Date("2017-06-16") 在IOS会出现NAN的情况所以对于时间转换需要另行封装,解 ...