上码
import React,{Component} from 'react';
class ErrorBoundary extends Component{
constructor(props){
super(props);
this.state = {}
}
componentDidCatch(error,info){
this.setState({
error: error,
errorInfo: info
})
}
render(){
if(this.state.errorInfo){
return <h1>Something went wrong.</h1>
}
return this.props.children;
}
}
export default ErrorBoundary;
使用
<ErrorBoundary>
<YourComponents />
</ErrorBoundary>