Roadmap › React (with Hooks, React 19 & Next.js basics) › Module: Building Real Apps
Lessons in this course (17)
15. Error boundaries, portals, Suspense and lazy loading
IntermediateMediumLesson 15 of 17
jsx
// Error boundary (still a class component, or use the react-error-boundary package)
class ErrorBoundary extends React.Component {
state = { hasError: false };
static getDerivedStateFromError() { return { hasError: true }; }
componentDidCatch(error, info) { logToServer(error, info.componentStack); }
render() { return this.state.hasError ? <p>Something went wrong.</p> : this.props.children; }
}
// Code splitting
const Reports = lazy(() => import("./Reports"));
<Suspense fallback={<Spinner />}><Reports /></Suspense>
// Portal: render outside the parent DOM (modals, tooltips)
createPortal(<Modal />, document.getElementById("modal-root"));Error boundaries catch errors during rendering, lifecycle and constructors of children — not in event handlers, async code or the boundary itself.