Error Boundaries
React error boundary se render errors pakar kar poori app crash hone se bachana seekhein, class component, react-error-boundary aur reset ke saath.
On this page
1. Error Boundary kya hai?
React mein agar kisi component ke render mein error aaye aur koi usay na pakre, to React poora root unmount kar deta hai. User ko khali safed screen dikhti hai.
Error Boundary JavaScript ke try/catch jaisa hai, lekin components ke tree ke liye.
2. Kyun zaroori hai?
| Bina Error Boundary | Error Boundary ke saath |
|---|---|
| Ek widget ka error, poori app safed | Sirf woh widget fallback dikhata hai |
| User ko pata nahi kya hua | Saaf message aur Try again button |
| Error kahin log nahi hota | componentDidCatch mein error reporting |
| Reload ke ilawa koi rasta nahi | Reset kar ke dobara render |
3. Kahan use hota hai?
| Jagah | Kyun |
|---|---|
| Poori app ke upar | Last line of defense, generic error page |
| Har route ya page ke gird | Ek page ka error baqi navigation ko na tode |
| Third-party widget, chart, map | Bahar ka code crash kare to sirf woh hissa |
use(promise) wale components | Rejected Promise ka error yahan aata hai |
Next.js App Router mein error.tsx file khud ek Error Boundary banati hai.
4. Kaise kaam karta hai?
5. Code example
Class component boundary
Error Boundary abhi bhi class component se hi banti hai, kyunke iske liye koi hook nahi hai.
import { Component, type ErrorInfo, type ReactNode } from "react";
type Props = { fallback: ReactNode; children: ReactNode };
type State = { hasError: boolean };
export class ErrorBoundary extends Component<Props, State> {
state: State = { hasError: false };
static getDerivedStateFromError(): State {
return { hasError: true };
}
componentDidCatch(error: Error, info: ErrorInfo) {
console.error(error, info.componentStack);
}
render() {
if (this.state.hasError) {
return this.props.fallback;
}
return this.props.children;
}
}<ErrorBoundary fallback={<p>Chart load nahi ho saka.</p>}>
<SalesChart />
</ErrorBoundary>react-error-boundary library ke saath reset
import { ErrorBoundary, type FallbackProps } from "react-error-boundary";
function Fallback({ error, resetErrorBoundary }: FallbackProps) {
return (
<div role="alert">
<p>Kuch ghalat ho gaya: {error instanceof Error ? error.message : "Unknown error"}</p>
<button onClick={resetErrorBoundary}>Try again</button>
</div>
);
}
export function Dashboard() {
return (
<ErrorBoundary FallbackComponent={Fallback}>
<SalesChart />
</ErrorBoundary>
);
}Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
getDerivedStateFromError | Error aate hi state hasError: true |
componentDidCatch | Logging ya Sentry jaisi service ko report |
info.componentStack | Kis component tree mein error aaya |
fallback | Error ke waqt dikhne wala UI |
resetErrorBoundary | State reset, children dobara render ki koshish |
React 19 mein createRoot ke onCaughtError aur onUncaughtError options bhi hain, jahan aap saare errors ek jagah report kar sakte hain.
6. Common mistakes
Galati: onClick ke error ke liye Error Boundary par bharosa
Why? Event handler render ke dauran nahi chalta, is liye boundary us error ko kabhi nahi dekhti.
Behtar: Handler ke andar try catch likhein aur error ko state mein rakh kar UI dikhayein.
Galati: Poori app ke liye sirf ek boundary
Why? Ek chhote widget ka error bhi poora page fallback mein badal deta hai.
Behtar: Top par ek boundary rakhein, aur ahem sections ya widgets ke gird alag boundaries.
Galati: Fallback mein reset ka koi rasta na dena
Why? User page reload karne par majboor hota hai aur form data kho deta hai.
Behtar: Try again button dein jo boundary reset kare, jaise react-error-boundary ka resetErrorBoundary.
Interview tip
Poocha ja sakta hai "Function component Error Boundary ban sakta hai?" Jawab: khud nahi, abhi tak sirf class component getDerivedStateFromError aur componentDidCatch de sakta hai. Function components ke liye react-error-boundary library wrapper deti hai.
7. Pehle aur baad mein kya seekhein?
| Stage | Topics |
|---|---|
| Pehle | Components, Conditional Rendering, Handling Events |
| Saath | Suspense & lazy(), The use() API |
| Baad mein | Server Components, Testing React Apps |
Final quick revision
| Point | Detail |
|---|---|
| Kya hai | Render errors pakar kar fallback dikhane wala component |
| Banta kaise hai | Class component, getDerivedStateFromError |
| Logging | componentDidCatch(error, info) |
| Nahi pakarta | Event handlers, async code, SSR, khud apna error |
| Easy option | react-error-boundary library |
| Next.js | error.tsx file |