Error Boundaries are React components that catch JavaScript errors anywhere in their child component tree and display a fallback UI instead of the component tree that crashed.
Error Boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them.
They don't catch errors in event handlers, async code, or server-side rendering.
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; }
static getDerivedStateFromError(error) { return { hasError: true }; }
componentDidCatch(error, errorInfo) { console.log('Error:', error); console.log('Error Info:', errorInfo); }
render() { if (this.state.hasError) { return <h1>Something went wrong.</h1>; }
} }
// Usage <ErrorBoundary> <MyComponent /> </ErrorBoundary>