๐ย Reference
๐ย Chapter
componentDidCatch(error, info)
getDerivedStateFromError(error)
โฃ
โฃ
โฃ
โฃ
getDerivedStateFromError์ componentDidCatch ์๋ช
์ฃผ๊ธฐ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.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;
}
}