Error Boundaries in React Prevent App Crashes

🚀 React Interview Question: What are Error Boundaries in React? 💡 Error Boundaries: Error Boundaries are components that catch errors in the UI and prevent the app from crashing. Instead of showing a broken screen, they display a fallback UI like “Something went wrong.” 🔹 Why use them? - prevent full app crash - show user-friendly error message - improve user experience 🔹 Example: class ErrorBoundary extends React.Component { state = { hasError: false }; static getDerivedStateFromError() { return { hasError: true }; } render() { if (this.state.hasError) { return <h1>Something went wrong </h1>; } return this.props.children; } } 🔹Note: Error Boundaries only work in class components ✅ If you are preparing for ReactJs interview, save this for your next interview! Follow Tarun Kumar for tech content, coding tips, and interview prep 🚀 #React #JavaScript #InterviewPrep #Frontend #Coding

  • text

To view or add a comment, sign in

Explore content categories