React › Error Boundaries

Where should you place error boundaries in a real React app?

Beginner

Answer

A good approach is placing one error boundary around your entire app as a safety net, and then placing smaller, more specific error boundaries around individual features or sections. This way, if one small part of the page breaks, the rest of the app keeps working normally instead of the whole page going blank.
<ErrorBoundary>
  <Header />
  <ErrorBoundary>
    <ProductRecommendations />
  </ErrorBoundary>
  <MainContent />
</ErrorBoundary>
Real-world example A news website wraps its unreliable third party advertisement widget in its own small error boundary, so if the ad script fails, only that specific ad space shows a fallback message while the actual news article keeps displaying normally.

Common follow-ups: How many error boundaries is too many for a typical page?;Should every single component have its own dedicated error boundary?