【发布时间】:2019-03-13 21:45:46
【问题描述】:
我找不到与地雷相关的情况,但是我的问题是TypeError: Cannot read property 'props' of undefined 的常见错误。
奇怪的是,这个错误只发生在我上面定义的方法render()。
在render() 内部,我可以毫无错误地访问。 React 开发工具显示我什至可以访问道具。
代码如下:
import { Route } from 'react-router-dom'
import AuthService from '../../utils/authentication/AuthService'
import withAuth from '../../utils/authentication/withAuth'
const Auth = new AuthService()
class HomePage extends Component {
handleLogout() {
Auth.logout()
this.props.history.replace('/login')
}
render() {
console.log(this.props.history)
return (
<div>
<div className="App-header">
<h2>Welcome {this.props.user.userId}</h2>
</div>
<p className="App-intro">
<button type="button" className="form-submit" onClick={this.handleLogout}>Logout</button>
</p>
</div>
)
}
}
export default withAuth(HomePage)
编辑:道歉。我也不想引起混淆,所以我会补充一点,我也在使用@babel/plugin-proposal-class-properties以避免this绑定。
【问题讨论】:
标签: reactjs react-props