๐Ÿ“šย Reference


๐Ÿ“œย Chapter


react-error-boundary

componentDidCatch(error, info)

getDerivedStateFromError(error)

Error

Suspense

โ€ฃ

โ€ฃ

โ€ฃ

โ€ฃ

ErrorBoundary


React ๊ธฐ๋ณธ ์—๋Ÿฌ ๋ฐ”์šด๋”๋ฆฌ (Class Component)


import React, { Component, ErrorInfo, ReactNode } from 'react';

interface Props { children: ReactNode; fallback: ReactNode; }
interface State { hasError: boolean; }

class ErrorBoundary extends Component<Props, State> {
  state: State = { hasError: false };

  // ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์—ฌ ๋‹ค์Œ ๋ Œ๋”๋ง ๋•Œ ํด๋ฐฑ UI๋ฅผ ๋ณด์—ฌ์คŒ
  static getDerivedStateFromError(_: Error): State {
    return { hasError: true };
  }

  // ์—๋Ÿฌ ์ •๋ณด๋ฅผ ๋กœ๊น… ์„œ๋น„์Šค(Sentry, LogRocket ๋“ฑ)์— ์ „์†ก
  componentDidCatch(error: Error, errorInfo: ErrorInfo) {
    console.error("Uncaught error:", error, errorInfo);
    // ์˜ˆ: Sentry.captureException(error);
  }

  render() {
    if (this.state.hasError) return this.props.fallback;
    return this.props.children;
  }
}