react withRouter
作用:withRouter 可以加工一般组件,让一般组件具备路由组件特有的API,其返回值是一个新组件
引入方式:
import {withRouter} from "react-router-dom"
案例:
import React, { Component } from 'react';
import {withRouter} from "react-router-dom"
class Header extends Component {
back = () => {
this.props.history.goBack()
}
go = () => {
this.props.history.goForward()
}
render() {
console.log(this.props)
return (
<div>
<div>这里是header</div>
<button onClick={this.back}>返回</button>
<button onClick={this.go}>前进</button>
<hr />
</div>
);
}
}
export default withRouter(Header);