QuickDevGuide

Home › React

Error Boundaries, Suspense & lazy — React

Error boundaries A rendering error in one component unmounts the whole app unless something catches it. An error boundary is a component that implements getDerivedStateFromError / componentDidCatch and renders a fallback UI. They must currently be class components (or use the react-error-boundary package). Error boundaries do not catch errors in event handlers, async code (setTimeout, fetch callbacks) or server rendering — use try/catch there. Code splitting with lazy + Suspense const Settings = lazy(() => import("./Settings")) loads the component's code only when it first renders. Wrap it in <Suspense fallback={<Spinner />}> to show something while loading. Place boundaries strategically: around each route, each independent widget (charts, comments), so one failure does not take down…

Key Takeaways