error.js 파일 컨벤션을 사용하면 중첩된 경로에서 예기치 않은 런타임 오류를 우아하게 처리할 수 있습니다.

라우트 세그먼트 안에 error.js 파일을 추가하고 React 컴포넌트를 내보내서 오류 UI를 만듭니다.

Untitled

'use client' // Error components must be Client Components
 
import { useEffect } from 'react'
 
export default function Error({
  error,
  reset,
}: {
  error: Error
  reset: () => void
}) {
  useEffect(() => {
    // Log the error to an error reporting service
    console.error(error)
  }, [error])
 
  return (
    <div>
      <h2>Something went wrong!</h2>
      <button
        onClick={
          // Attempt to recover by trying to re-render the segment
          () => reset()
        }
      >
        Try again
      </button>
    </div>
  )
}

error.js 작동 방식

Untitled