๐Ÿ“šย Reference


๐Ÿ“œย Chapter


react-error-boundary

componentDidCatch(error, info)

getDerivedStateFromError(error)

Error

Suspense

ErrorBoundary


์‚ฌ์šฉ๋ฒ•


import React from "react";

class ErrorBoundary extends React.Component<
  { fallback: React.ReactNode },
  { hasError: boolean }
> {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error: Error) {
    // ์—๋Ÿฌ ๊ฐ์ง€ ์‹œ state ๋ณ€๊ฒฝ โ†’ fallback ๋ Œ๋”๋ง
    return { hasError: true };
  }

  componentDidCatch(error: Error, info: React.ErrorInfo) {
    console.error("Error caught by boundary:", error, info);
  }

  render() {
    if (this.state.hasError) {
      return this.props.fallback;
    }
    return this.props.children;
  }
}
import React from "react";
import ErrorBoundary from "./ErrorBoundary";
import BrokenComponent from "./BrokenComponent";

export default function App() {
  return (
    <ErrorBoundary fallback={<h2>๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค ๐Ÿ˜ฅ</h2>}>
      <BrokenComponent />
    </ErrorBoundary>
  );
}