问题

当我们使用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 控制路由跳转的更多相关文章

  1. react项目中引入了redux后js控制路由跳转方案

    如果你的项目中并没有用到redux,那本文你可以忽略 问题引入 纯粹的单页面react应用中,通过this.props.history.push('/list')就可以进行路由跳转,但是加上了redu ...

  2. [Web 前端] React Router v4 入坑指南

    cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...

  3. react router 4.0以上的路由应用

    thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr& ...

  4. React-Router JS控制路由跳转

    React-Router JS控制路由跳转 时间: 2016-04-12 15:01:20 作者: zhongxia React-Router 控制路由跳转的方式,目前知道的有两种[Link 链接, ...

  5. React Router V4发布

    React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...

  6. [React Router v4] Redirect to Another Page

    Overriding a browser's current location without breaking the back button or causing an infinite redi ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. ubuntu下安装bin文件

    从Java官网下载的安装文件,有的只有bin文件,没有.tar.gz文件. ①进入设备终端,通过sudo -s或su回车,切换到管理员用户:②输入管理员密码然后回车:③输入sudo chmod +x ...

  2. XmlHelpers

    最近处理数据的时候用到了Xml和其他数据之间的转换,所以整理了一些方法. class XMLHelper { /// <summary> /// 读取xml模板 /// </summ ...

  3. [No0000128]SQL纵表与横表互转

    1.纵表转横表: 纵表结构:Table1 转换后的横表结构: Sql示例代码: select username, sum(case Course when '语文' then Grade else 0 ...

  4. 区块链共识机制:POW、POS、DPOS、PBFT、POOL

    共识机制作为区块链的关键技术之一,在业务吞吐量.交易速度.不可篡改性.准入门槛等等方面发挥重要的作用. 区块链是去中心化的,没有中心记账节点,所以需要全网对账本达成共识.目前有POW.POS.DPOS ...

  5. Microsoft .NET Framework

    Microsoft .NET Framework是用于Windows的新托管代码编程模型.它将强大的功能与新技术结合起来,用于构建具有视觉上引人注目的用户体验的应用程序,实现跨技术边界的无缝通信,并且 ...

  6. Chap5:操作文件和目录[The Linux Command Line]

    Wildcards Wildcard Meaning * Matches any characters ? Matches any single character [characters] Matc ...

  7. Flink - InputGate

    初始化 Task List<InputGateDeploymentDescriptor> consumedPartitions = tdd.getInputGates(); // Cons ...

  8. LeetCode 657 Robot Return to Origin 解题报告

    题目要求 There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of it ...

  9. 《Redis 集群》

    由于集群这章节内容较多,也比较重要,所以单独拉出来,做一个小章节. 1:如何搭建一个集群? - 环境为 Ubuntu16.04 - 这里我预计使用 9001 - 9006 端口,生成一个 6 台机器的 ...

  10. 《Mysql ALTER基本操作》

    一:ALTER 添加单列 - 语法 - ALTER TABLE 表名 ADD 列名 定义类型 [FIRST(列将加入最上方) | AFTER 字段名(列加入某某字段之后) ] - 示例 `user` ...