react之错误边界
1、理解:用来捕获后代组件错误,渲染出备用页面
2、特点:
只能捕获后代组件生命周期产生的错误,不能捕获自己组件产生的错误和其他组件在合成事件、定时器中产生的错误
3、使用方式:
getDerivedStateFromError配合componentDidCatch
static getDerivedStateFromError(error) {
console.log(error);
//在render之前触发
//返回新的state
return {
hasError: true
}
}
componentDidCatch( error, info) {
//统计页面的错误,发送请求发送到后台去
console.log(error,info)
}