- 一般组件经过withRouter的加工,也有了路由组件history身上特有的那些api
- withRouter专门用于在一般组件身上使用路由组件的问题
- withRouter的返回值是一个新组件
import React, {
Component } from 'react'
import {
withRouter} from 'react-router-dom'
class Header extends Component {
back=()=>{
this.props.history.goBack()
}
forward=()=>{
this.props.history.goForward()
}
go=()=>{
this.props.history.go(-2)
}
render() {
console.log('Header组件收到的props是:',this.props)
return (
<div className="page-header">
<h2>React Router Demo</h2>
<button onClick={
this.back}>回退</button>
<button onClick={
this.forward}>前进</button>
<button onClick={
this.go}>go</button>
</div>
)
}
}
export default withRouter(Header)