ErrorBoundary.tsx 883 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import React, { ClassicComponent, ErrorInfo } from "react";
  2. import Modal from "../portals/modal";
  3. export class ErrorBoundary extends React.Component<any, any> {
  4. constructor(props: any) {
  5. super(props);
  6. }
  7. state = {
  8. currentError: null,
  9. errorInfo: null,
  10. };
  11. componentDidCatch(error: Error, info: ErrorInfo) {
  12. this.setState({
  13. currentError: error,
  14. errorInfo: info,
  15. });
  16. }
  17. render() {
  18. return (
  19. <>
  20. {this.props.children}
  21. {this.state.currentError && (
  22. <Modal
  23. key="Error"
  24. title={"Error"}
  25. staticBackdrop
  26. accept={() =>
  27. this.setState({ currentError: null, errorInfo: null })
  28. }
  29. >
  30. <p style={{ color: "red" }}>{this.state.errorInfo}</p>
  31. </Modal>
  32. )}
  33. </>
  34. );
  35. }
  36. }
  37. export default ErrorBoundary;