ErrorBoundary.tsx 888 B

12345678910111213141516171819202122232425262728293031323334
  1. import React, { Component } from 'react';
  2. export default class ErrorBoundary extends Component<{}, any> {
  3. constructor(props) {
  4. super(props);
  5. this.state = { error: null, errorInfo: null };
  6. }
  7. componentDidCatch(error, errorInfo) {
  8. // Catch errors in any components below and re-render with error message
  9. this.setState({
  10. error: error,
  11. errorInfo: errorInfo,
  12. });
  13. }
  14. render() {
  15. if (this.state.errorInfo) {
  16. // Error path
  17. return (
  18. <div className="explore-container">
  19. <h3>An unexpected error happened.</h3>
  20. <details style={{ whiteSpace: 'pre-wrap' }}>
  21. {this.state.error && this.state.error.toString()}
  22. <br />
  23. {this.state.errorInfo.componentStack}
  24. </details>
  25. </div>
  26. );
  27. }
  28. // Normally, just render children
  29. return this.props.children;
  30. }
  31. }