Skip to content

Latest commit

ย 

History

History
90 lines (73 loc) ยท 4.09 KB

errorBoundary.md

File metadata and controls

90 lines (73 loc) ยท 4.09 KB

๐Ÿ’ป ErrorBoundary ์ฃผ์š” ๋‚ด์šฉ

ErrorBoundary

  • ๋ฆฌ์•กํŠธ์—์„œ ErrorBoundary๋Š” ํ•˜์œ„ ๊ตฌ์„ฑ ์š”์†Œ ํŠธ๋ฆฌ์˜ ์ž„์˜์˜ ์œ„์น˜์—์„œ JavaScript ์—๋Ÿฌ๋ฅผ ๊ฐ์ง€ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ด๋ฉฐ, ์ถฉ๋Œํ•œ ์ปดํฌ๋„ŒํŠธ ๋Œ€์‹  Fallback UI๋ฅผ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์„ ์–ธ์ ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • UI์˜ ์ผ๋ถ€๋ถ„์— ์กด์žฌํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—๋Ÿฌ๊ฐ€ ์ „์ฒด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ค‘๋‹จ์‹œํ‚ค์ง€ ์•Š๊ธฐ ์œ„ํ•ด ErrorBoundary๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ์—๋Ÿฌ ๋ฐ”์šด๋”๋ฆฌ๋Š” ๋ Œ๋”๋ง ์ค‘, ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ๋ฐ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ ํŠธ๋ฆฌ์˜ ์–ด๋””์—์„œ๋“  ์—๋Ÿฌ๋ฅผ ํƒ์ง€ํ•ฉ๋‹ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ, ์ดˆ๊ธฐ ๋ Œ๋”๋ง, ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€ ์˜ค๋ฅ˜๋‚˜ ๋น„๋™๊ธฐ ์ฝ”๋“œ์—์„œ ์˜ค๋ฅ˜๋ฅผ ๊ฐ์ง€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์˜ค๋ฅ˜ ๊ฒฝ๊ณ„๋Š” ํŠธ๋ฆฌ์—์„œ ์•„๋ž˜ ๊ตฌ์„ฑ ์š”์†Œ์˜ ์˜ค๋ฅ˜๋งŒ ๊ฐ์ง€ํ•˜๊ณ , ์˜ค๋ฅ˜ ๊ฒฝ๊ณ„ ์ž์ฒด์˜ ์˜ค๋ฅ˜๋Š” ๊ฐ์ง€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์„ ์–ธํ˜•์ด๋ž€?

  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์„ ์–ธํ˜•์€ ๊ฒฐ๊ณผ๋ฌผ์—๋งŒ ์ง‘์ค‘ํ•˜๊ณ  ๋ณต์žกํ•œ ๊ณผ์ •์„ ์ถ”์ƒํ™”ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • ์„ ์–ธํ˜• ๋ฐ˜๋Œ€์˜ ๊ฐœ๋…์œผ๋กœ ๋ช…๋ นํ˜•์ด ์žˆ์œผ๋ฉฐ, ๋ช…๋ นํ˜•์€ ๊ฒฐ๊ณผ๋ฌผ๋ณด๋‹ค๋Š” ๊ทธ ๊ณผ์ •์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ช…๋ นํ˜•์˜ ๊ฒฝ์šฐ ํ•œ ์ค„์”ฉ ์ฝ์–ด ๋‚˜๊ฐ€๋ฉด์„œ ๋‹ค์Œ์— ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ• ์ง€ ์ถ”์ธกํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด, ์„ ์–ธํ˜•์˜ ๊ฒฝ์šฐ ์ž์„ธํ•œ ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด๋”๋ผ๋„ ์ฝ”๋“œ๋งŒ ๋ณด๊ณ ๋„ ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ• ์ง€ ์˜ˆ์ธก์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
// ๋ช…๋ นํ˜•
function double(arr) {
  let results = [];
  for (let i = 0; i < arr.length; i++) {
    results.push(arr[i] * 2);
  }
  return results;
}
// ์„ ์–ธํ˜•
function double(arr) {
  return arr.map((item) => item * 2);
}
// ๋ฆฌ์•กํŠธ
<QueryErrorBoundary
  fallback={
    <DefaultFallback onResetAction={() => location.replace("/start")} />
  }
>
  {/* ... */}
</QueryErrorBoundary>
  • ๋ฆฌ์•กํŠธ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์šฐ๋ฆฌ๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด DefaultFallback์„ ๋ณด์—ฌ์ฃผ๋Š”๊ตฌ๋‚˜ ๋ฐ”๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • QueryErrorBoundary๊ฐ€ ๋‚ด๋ถ€์—์„œ ์–ด๋–ค ๋กœ์ง์ด ๋™์ž‘ํ•˜๋Š”์ง€ ์šฐ๋ฆฌ๋Š” ์‹ ๊ฒฝ ์“ฐ์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๋‚ด๋ถ€ ๋ณต์žก์„ฑ์„ ์ถ”์ƒํ™”ํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ์„ ์–ธํ˜•์ž…๋‹ˆ๋‹ค.

ErrorBoundary ๊ธฐ๋ณธ ์˜ˆ์ œ

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    // ๋‹ค์Œ ๋ Œ๋”๋ง์—์„œ ํด๋ฐฑ UI๊ฐ€ ๋ณด์ด๋„๋ก ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // ์—๋Ÿฌ ๋ฆฌํฌํŒ… ์„œ๋น„์Šค์— ์—๋Ÿฌ๋ฅผ ๊ธฐ๋กํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
    logErrorToMyService(error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      // ํด๋ฐฑ UI๋ฅผ ์ปค์Šคํ…€ํ•˜์—ฌ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
      return <h1>Something went wrong.</h1>;
    }

    return this.props.children;
  }
}
  • ์œ„์— ์ฝ”๋“œ๋Š” ๋ฆฌ์•กํŠธ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ์ œ๊ณตํ•ด ์ฃผ๋Š” ๊ธฐ๋ณธ ErrorBoundary ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
  • ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ์ธ getDerivedStateFromError()์™€ ComponentDidCatch()์ค‘ ํ•˜๋‚˜(ํ˜น์€ ๋‘˜ ๋‹ค)๋ฅผ ์ •์˜ํ•˜๋ฉด ์—๋Ÿฌ ๊ฒฝ๊ณ„๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
    • ์—๋Ÿฌ ๋ฐœ์ƒํ•œ ๋’ค์— ํด๋ฐฑ UI ๋ Œ๋”๋ง์„ ํ•˜๋ ค๋ฉด static getDerivedStateFromError๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
      • ํ•ด๋‹น ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋Š” ํ•˜์œ„์˜ ์ž์† ์ปดํฌ๋„ŒํŠธ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์˜ค๋ฅ˜๋ฅผ ์ „๋‹ฌ๋ฐ›๊ณ , ๊ฐฑ์‹ ๋œ state ๊ฐ’์„ ๋ฐ˜๋“œ์‹œ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ์—๋Ÿฌ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๋ ค๋ฉด componentDidCatch๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
      • ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋Š” ์ž์† ์ปดํฌ๋„ŒํŠธ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋ฉฐ, 2๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌ๋ฐ›์Šต๋‹ˆ๋‹ค.
      • error - ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜, info - ์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œ์ผฐ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ํฌํ•จํ•œ componentStack ํ‚ค๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด
  • ์—๋Ÿฌ ๋ฐ”์šด๋”๋ฆฌ๋Š” ํŠธ๋ฆฌ ๋‚ด์—์„œ ํ•˜์œ„์— ์กด์žฌํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์—๋Ÿฌ๋งŒ์„ ํฌ์ฐฉํ•ฉ๋‹ˆ๋‹ค
  • ์—๋Ÿฌ ๊ฒฝ๊ณ„์˜ ๊ฐ ์œ„์ ฏ์„ ์—๋Ÿฌ ๊ฒฝ๊ณ„๋กœ ๊ฐ์‹ธ์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์ด ์ถฉ๋Œํ•˜์ง€ ์•Š๋„๋ก ๋ณดํ˜ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.