react-router v4 使用 history 控制路由跳转
问题
当我们使用react-router v3的时候,我们想跳转路由,我们一般这样处理
我们从react-router导出browserHistory。
我们使用browserHistory.push()等等方法操作路由跳转。
类似下面这样
import browserHistory from 'react-router';
export function addProduct(props) {
return dispatch =>
axios.post(`xxx`, props, config)
.then(response => {
browserHistory.push('/cart'); //这里
});
}
问题来了,在react-router v4中,不提供browserHistory等的导出~~
那怎么办?我如何控制路由跳转呢???
解决方法
- 使用 withRouter
withRouter高阶组件,提供了history让你使用~
import React from "react";
import {withRouter} from "react-router-dom"; class MyComponent extends React.Component {
...
myFunction() {
this.props.history.push("/some/Path");
}
...
}
export default withRouter(MyComponent);
这是官方推荐做法哦。但是这种方法用起来有点难受,比如我们想在redux里面使用路由的时候,我们只能在组件把history传递过去。。
就像问题章节的代码那种场景使用,我们就必须从组件中传一个history参数过去。。。
- 使用 Context
react-router v4 在 Router 组件中通过Contex暴露了一个router对象~
在子组件中使用Context,我们可以获得router对象,如下面例子~
import React from "react";
import PropTypes from "prop-types"; class MyComponent extends React.Component {
static contextTypes = {
router: PropTypes.object
}
constructor(props, context) {
super(props, context);
}
...
myFunction() {
this.context.router.history.push("/some/Path");
}
...
}
当然,这种方法慎用~尽量不用。因为react不推荐使用contex哦。在未来版本中有可能被抛弃哦。
- hack
其实分析问题所在,就是v3中把我们传递给Router组件的history又暴露出来,让我们调用了
而react-router v4 的组件BrowserRouter自己创建了history,
并且不暴露出来,不让我们引用了。尴尬~
我们可以不使用推荐的BrowserRouter,依旧使用Router组件。我们自己创建history,其他地方调用自己创建的history。看代码~
下面是我目前所使用的办法
我们自己创建一个history
// src/history.js
import createHistory from 'history/createBrowserHistory'; export default createHistory();
新的版本需要这样引入
import { createHashHistory,createBrowserHistory } from 'history'; // 是hash路由 history路由 自己根据需求来定
ts引入
import * as createHistory from "history";
export default createHistory.createBrowserHistory();
我们使用Router组件
// src/index.js
import { Router, Link, Route } from 'react-router-dom';
import history from './history';
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
...
</Router>
</Provider>,
document.getElementById('root'),
);
其他地方我们就可以这样用了
import history from './history';
export function addProduct(props) {
return dispatch =>
axios.post(`xxx`, props, config)
.then(response => {
history.push('/cart'); //这里
});
}
this.props.history.push("/two")
react-router v4推荐使用BrowserRouter组件,而在第三个解决方案中,我们抛弃了这个组件,又回退使用了Router组件。
我目前也没有更好的办法了
react-router v4 使用 history 控制路由跳转的更多相关文章
- react项目中引入了redux后js控制路由跳转方案
如果你的项目中并没有用到redux,那本文你可以忽略 问题引入 纯粹的单页面react应用中,通过this.props.history.push('/list')就可以进行路由跳转,但是加上了redu ...
- [Web 前端] React Router v4 入坑指南
cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
- react router 4.0以上的路由应用
thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr& ...
- React-Router JS控制路由跳转
React-Router JS控制路由跳转 时间: 2016-04-12 15:01:20 作者: zhongxia React-Router 控制路由跳转的方式,目前知道的有两种[Link 链接, ...
- React Router V4发布
React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...
- [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] 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] 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 ...
随机推荐
- git 搭建本地仓库
文档 创建仓库 mkdir project cd project/ git init git remote add origin /d/project/.git // 仓库创建好了 echo hell ...
- 在AJAX里 使用【 XML 】 返回数据类型 实现简单的下拉菜单数据
在AJAX里 使用XML返回数据类型 实现简单的下拉菜单数据 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
- PHP链接MySQL,查询数据库内容,删除数据库内容。。。记住链接公式!!!
//扩展类叫MySQLi MySQL是数据库,MySQLi是扩展 Id地址本地网络服务器的地址localhost 如果想链接别人的输入他的服务器id地址. //root代表的是数据库名, //poss ...
- span 英文数字保持一行,中文自动换行
html 中 span 换行规则如下: span不换行默认只针对英文有效 如果想对中文设置有效需要添加样式 style="white-space:nowrap;" 默认的情况是这样 ...
- CCPC-Wannafly Winter Camp Day7 D---二次函数【数论】【构造】
题意: 有三个二次函数,分别是$x^2 + a_1x + b_1$, $x^2 + a_2x + b_2$, $x^2 + a_3x + b_3$ 现在要找三个整数$x_1, x_2, x_3$, 使 ...
- [No0000139]轻量级文本编辑器,Notepad最佳替代品:Notepad++
在详细介绍Notepad++之前,先来解释一下,为何要选择Notepad++,即把常见的一些文本编辑器和Notepad++比较,看看其有哪点好: 常见的文本编辑器有很多,此处,只提及Notepad,N ...
- UDP,TCP的套接字编程的Python实现
UDP,TCP的套接字编程的Python实现 套接字:连接应用层和运输层,应用层的网络应用程序使用IP地址+端口号来标识自己,然后通过套接字调用运输层为其服务,网络应用程序只能指定自己要使用的网络类型 ...
- 8 queen暴力解决法
很容易看出来,在每一行和每一列上有且只能有一个皇后,因此较为复杂的判断就是对角线了.维基百科的页面上有一个非常暴力但是写起来非常简单的解法: 1 2 3 4 5 6 7 8 from itertool ...
- 第一章:深入.NET框架
1..net框架结构 主要包含公共语言运行时(CLR)和框架类库(.NET Framework 类库 ,FCL) 2.CLR 1.对于一个将要面向.NET平台进行开发的人来说,了解一下.NET平台的 ...
- wordpress如何屏蔽wp-json(禁用REST API)
最近网友问ytkah怎么在网站日志文件中发现蜘蛛爬行了很多次的/wp-json/目录,在robots文件中disallow掉了爬虫还是访问了那个目录,能不能直接在程序中直接改呢?通过查询相关文档发现W ...