What if your app never crashes on users, no matter what goes wrong?
Why Handling runtime errors in React? - Purpose & Use Cases
Imagine your React app crashes completely when a small mistake happens, like a missing value or a broken API call, leaving users staring at a blank screen.
Manually checking every possible error everywhere is exhausting and easy to forget. Without proper error handling, your app becomes unreliable and frustrating for users.
React's error boundaries catch runtime errors in components and let you show a friendly message instead of a crash, keeping your app stable and user-friendly.
componentDidCatch(error) { console.log(error); } // but UI still breaksclass ErrorBoundary extends React.Component { state = { hasError: false }; static getDerivedStateFromError() { return { hasError: true }; } componentDidCatch(error, info) { console.log(error, info); } render() { return this.state.hasError ? <h1>Something went wrong.</h1> : this.props.children; } } // Usage: <ErrorBoundary>{children}</ErrorBoundary>
You can build resilient apps that gracefully handle mistakes and keep users happy even when things go wrong.
When a user uploads a bad file or a network request fails, your app shows a helpful message instead of crashing, guiding users to fix the problem.
Runtime errors can crash your app unexpectedly.
Manual error checks are hard and incomplete.
React error boundaries catch errors and show fallback UI.