import * as React from 'react'; interface Props {} class ErrorBoundary extends React.Component { state = { hasError: false }; constructor(props: Props) { super(props); } static getDerivedStateFromError(error: Error) { return { hasError: true }; } componentDidCatch(error: Error, errorInfo: any) { console.log('error', error, 'errorInfo', errorInfo); } render() { if (this.state.hasError) { return
出错啦, 请查看控制台
; } else { return this.props.children; } } } export default ErrorBoundary;