Web Development
React.js
Subjective
Sep 28, 2025
What are React error boundaries and how to implement them?
Detailed Explanation
Error boundaries catch JavaScript errors in component trees and display fallback UI. Implement using class components with componentDidCatch() and getDerivedStateFromError().
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, errorInfo);
}
render() {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts