class ErrorBoundary extends React.Component{
constructor(props){
super(props)
this.state={
hasErrpr:false
}
}
static getDerivedStateFromError(error){
// 更新state 使下一次渲染能够显示降级后的UI
return {hasError:true}
}
componentDIdCatch(error,errorInfo){
// 将错误日志上报给服务器
}
render(){
if(this.state.hasError){
return <h1>something went wrong</h1>
}
}
return this.props.children
}