问题:在react子组件中,_this.props.history为undefined,
解决方法:引入withRouter,导出时调用。
import { withRouter } from 'react-router-dom';
原理:高阶组件中的withRouter
, 作用是将一个组件包裹进Route
里面, 然后react-router
的三个对象history, location, match
就会被放进这个组件的props
属性中.如果我们某个东西不是一个Router
, 但是我们要依靠它去跳转一个页面, 比如点击页面的logo
, 返回首页, 这时候就可以使用withRouter
来做.