Error Handling in React
Key Concepts
- Error Boundaries
- componentDidCatch
- static getDerivedStateFromError
- Error Handling Strategies
- Logging Errors
- Fallback UI
- Error Boundary Limitations
- Global Error Handling
Error Boundaries
Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of crashing the whole application.
Example:
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: true }; } componentDidCatch(error, info) { console.error('Error caught by boundary:', error, info); } render() { if (this.state.hasError) { return <h1>Something went wrong.</h1>; } return this.props.children; } }
componentDidCatch
This lifecycle method is called after an error has been thrown by a descendant component. It allows you to log the error and perform any necessary cleanup.
Example:
componentDidCatch(error, info) { console.error('Error caught by boundary:', error, info); }
static getDerivedStateFromError
This static method is called after an error has been thrown by a descendant component. It allows you to update the state to display a fallback UI.
Example:
static getDerivedStateFromError(error) { return { hasError: true }; }
Error Handling Strategies
Effective error handling strategies include using error boundaries, validating input, and handling asynchronous errors. These strategies ensure that your application remains stable and user-friendly.
Example:
function fetchData() { try { const response = await fetch('https://api.example.com/data'); if (!response.ok) { throw new Error('Network response was not ok'); } const data = await response.json(); return data; } catch (error) { console.error('Error fetching data:', error); return null; } }
Logging Errors
Logging errors helps in diagnosing and fixing issues. You can log errors to the console, a file, or an external service like Sentry.
Example:
componentDidCatch(error, info) { console.error('Error caught by boundary:', error, info); logErrorToService(error, info); }
Fallback UI
A fallback UI is a user-friendly message or component displayed when an error occurs. It provides feedback to the user and prevents the application from crashing.
Example:
render() { if (this.state.hasError) { return <h1>Something went wrong.</h1>; } return this.props.children; }
Error Boundary Limitations
Error boundaries do not catch errors for:
- Event handlers
- Asynchronous code (e.g., setTimeout or requestAnimationFrame callbacks)
- Server-side rendering
- Errors thrown in the error boundary itself
Global Error Handling
Global error handling involves catching and handling errors that occur outside of React components, such as in event handlers or asynchronous code.
Example:
window.addEventListener('error', (event) => { console.error('Global error:', event.error); });
Analogies
Think of error handling in React as a safety net in a circus act. The performers (components) can make mistakes, but the safety net (error boundaries) catches them and prevents a catastrophic fall (application crash). The net also provides a soft landing (fallback UI) and helps the performers recover (log and handle errors).
Another analogy is a doctor's office. When a patient (component) has an issue, the doctor (error boundary) diagnoses the problem (logs the error), provides treatment (displays a fallback UI), and ensures the office (application) continues to function smoothly.