原文地址: https://github.com/brickspert... (若是你以为对你有帮助,能够去github上点个star哦。)javascript
github上会更新,这里不更新java
当咱们使用react-router v3
的时候,咱们想跳转路径,咱们通常这样处理react
咱们从react-router
导出browserHistory
。ios
咱们使用browserHistory.push()
等等方法操做路由跳转。git
相似下面这样github
import browserHistory from 'react-router'; export function addProduct(props) { return dispatch => axios.post(`xxx`, props, config) .then(response => { browserHistory.push('/cart'); //这里 }); }
but!! 问题来了,在react-router v4
中,不提供browserHistory
等的导出~~redux
那怎么办?我如何控制路由跳转呢???axios
withRouter
高阶组件,提供了history
让你使用~react-router
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
传递过去。。dom
就像问题章节的代码那种场景使用,咱们就必须从组件中传一个history
参数过去。。。
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
哦。在将来版本中有可能被抛弃哦。
其实分析问题所在,就是v3
中把咱们传递给Router
组件的history
又暴露出来,让咱们调用了~~
而react-router v4
的组件BrowserRouter
本身建立了history
,
而且不暴露出来,不让咱们引用了。尴尬~
咱们能够不使用推荐的BrowserRouter
,依旧使用Router
组件。咱们本身建立history
,其余地方调用本身建立的history
。看代码~
咱们本身建立一个history
// src/history.js import createHistory from 'history/createBrowserHistory'; export default createHistory();
咱们使用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'); //这里 }); }
BrowserRouter
确实,react-router v4
推荐使用BrowserRouter
组件,而在第三个解决方案中,咱们抛弃了这个组件,又回退使用了Router
组件。
怎么办。 你去看看BrowserRouter
的源码,我以为你就豁然开朗了。
源码很是简单,没什么东西。咱们彻底本身写一个BrowserRouter
组件,而后替换第三种解决方法中的Router
组件。嘿嘿。
讲到这里也结束了,我本身目前在使用第三种方法,虽然官方推荐第一种,我以为用着比较麻烦唉。~