Skip to main content
InterviewHubPRO
Section Overview
Q174

Question 174 of 407

Part 8: Advanced React Patterns, Portals & Performance (152 - 261)

43%
React Core & Hooks (407 Qs)•Part 8: Advanced React Patterns, Portals & Performance (152 - 261)

What is the proper placement for error boundaries?

junior LevelCode ExampleFormsPerformance

The granularity of error boundaries usage is up to the developer based on project needs. You can follow either of these approaches,

  1. You can wrap top-level route components to display a generic error message for the entire application.
  2. You can also wrap individual components in an error boundary to protect them from crashing the rest of the application.

Practical Code Example

jsx
// Strategic Placement: Wrap critical widgets independently to avoid crashing entire page
function Dashboard() {
  return (
    <div>
      <Header />
      <ErrorBoundary fallback={<WidgetError />}>
        <AnalyticsChart />
      </ErrorBoundary>
      <ErrorBoundary fallback={<FeedError />}>
        <LiveFeed />
      </ErrorBoundary>
    </div>
  );
}