Error Boundary

حد الخطأ (Error Boundary)

Intermediatereact1 min read
errorboundaryreact error boundarycatch rendering errorfallback ui

Definition

A React class component or Next.js error.tsx that catches JavaScript errors in child components and shows a fallback UI.

مكوّن React كلاسي أو error.tsx في Next.js يلتقط أخطاء JavaScript في المكونات الفرعية ويعرض واجهة مستخدم بديلة.

Why It Matters

Without error boundaries, a single component throwing during render crashes the entire page. The `error.tsx` and `not-found.tsx` files in 404Fault prevent blank screen failures.

بدون حدود الأخطاء، مكوّن واحد يرمي خطأ أثناء التصيير يُعطّل الصفحة بأكملها. ملفات `error.tsx` و`not-found.tsx` في 404Fault تمنع إخفاقات الشاشة البيضاء.

Full Definition

Error boundaries catch rendering errors in their child component tree and display a fallback UI instead of crashing the whole app. Implemented as a class component with `componentDidCatch` and `getDerivedStateFromError`. In Next.js App Router, `error.tsx` files serve as error boundaries automatically. `not-found.tsx` handles 404s. 404Fault has a custom `not-found.tsx` that shows branded 404 UI.
حدود الأخطاء تلتقط أخطاء التصيير في شجرة المكونات الفرعية وتعرض واجهة مستخدم بديلة بدلاً من تعطل التطبيق بأكمله. في Next.js App Router، ملفات `error.tsx` تعمل كحدود أخطاء تلقائيًا.

Example Usage

// app/glossary/error.tsx — Next.js automatically uses this as an error boundary for the /glossary route

// app/glossary/error.tsx — Next.js يستخدم هذا تلقائيًا كحد خطأ لمسار /glossary

Knowledge Graph

Avoid these mistakes when using Error Boundary:

1

Forgetting that error boundaries don't catch async errors — those need try/catch in useEffect or event handlers

2

Not providing a useful fallback UI — blank screens are worse than error messages

Sign in to unlock guided AI explanations from AI Teacher.

Generate a Prompt

Copy this prompt and use it directly with any AI model — no setup needed.

Ready-to-Use Prompt
Help me build a project using Error Boundary.

Explain:
1. What is Error Boundary and why it matters
2. The core architecture and required tools
3. Step-by-step implementation plan
4. Common mistakes to avoid: Forgetting that error boundaries don't catch async errors — those need try/catch in useEffect or event handlers, Not providing a useful fallback UI — blank screens are worse than error messages
5. Best practices and production tips

Official Resources