路由跳转我们可以用Link标签,但是Link标签不会校验对错,有时候会不符合业务,所以今天我们就用事件来跳转路由
react-router版本 “react-router-dom”: “^4.3.1”
首先引入withRouter
import {withRouter} from 'react-router-dom';
并切在导出时候用这个方法包裹住
export default connect(function (state, props){
return state;
}, {
addItem(item){
return {
type: ADD_ITEM,
item
}
}
})(withRouter(Panel));
上面代码主要是结合redux,connect和withRouter谁包住谁都是可以的如果不用redux的话
export default (withRouter(Panel));
这个时候this.props下面就会有history对象了 大家可以自己打印一下
我们通常会用其中的两个方法 .push(会保存历史记录) 与 .replace(替换的意思,不会保存历史记录)
this.props.history.replace('/');
这样就可以通过事件来进行路由跳转了