【发布时间】:2021-02-03 02:40:41
【问题描述】:
我收到错误属性 errorOccurred 在类型 Readonly<{}> 上不存在
即使在谷歌和类似问题的帮助下,我也找不到解决方案。
代码如下:
class ErrorHandler extends React.Component {
constructor(props) {
super(props)
this.state = { errorOccurred: false }
}
componentDidCatch(error, info) {
this.setState({ errorOccurred: true })
}
render() {
return this.state.errorOccurred ? <h1>Something went wrong!</h1> :
this.props.children
}
}
【问题讨论】:
-
定义组件时不提供通用属性或状态类型。
-
在我看来您使用的是 TypeScript,但您还没有定义组件状态的类型。
-
如果将示例代码放在codesandbox 中,则非常适合调试。
标签: reactjs