withRouter解决一般组件使用路由组件中的AP I
实例代码如下
import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'
class Header extends Component {
handeleForward = () => {
this.props.history.goForward()
}
handleBack = () => {
this.props.history.goBack()
}
handleGo = () => {
this.props.history.go(2)
}
render() {
return (
<div className="page-header">
<h2> React Router Demo</h2>
<button onClick={this.handeleForward}>前进</button>
<button onClick={this.handleBack}>后退</button>
<button onClick={this.handleGo}>go</button>
</div>
)
}
}
export default withRouter(Header)

浙公网安备 33010602011771号