React路由报错 Cannot read property 'push' of undefined

2021-04-20 19:18:43
React路由报错 `Cannot read property 'push' of undefined`,解决方法是加上`withRouter`,代码示例如下 ``` import React from 'react' import {withRouter} from 'react-router-dom' class Demo extends React.Component { handleClick(){ this.props.history.push("/login") } render() { return ( <button onClick={this.handleClick}>去登录</button> ) } } export default withRouter(Demo) ```