DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

React Error Boundaries with Functional Components: What Works and How to Build One

React’s built-in error-boundary APIs require a class, but your children and fallback can be function components. Here’s the implementation, catch scope, placement guidance, and React 19 reporting behavior.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can you write a React error boundary as a function component? Not with React’s built-in APIs: there is no function-component equivalent for getDerivedStateFromError or componentDidCatch. Use a small class boundary or the react-error-boundary package React names as an alternative. Your application’s ordinary UI, including the children and fallback, can still use function components and hooks.

Build a reusable class boundary

A boundary catches render errors from descendants and switches to fallback UI. Keep the state update in the static lifecycle method; use componentDidCatch for reporting side effects. React says getDerivedStateFromError should be pure, and recommends it rather than calling setState in componentDidCatch to select the fallback.

As an Amazon Associate I earn from qualifying purchases.

import { Component } from 'react';

class ErrorBoundary extends Component {
  state = { hasError: false };

  static getDerivedStateFromError(error) {
    return { hasError: true };
  }

  componentDidCatch(error, info) {
    reportErrorToService(error, info.componentStack);
  }

  render() {
    if (this.state.hasError) {
      return this.props.fallback;
    }

    return this.props.children;
  }
}

function ProductPanel() {
  return <section>Product details</section>;
}

function App() {
  return (
    <ErrorBoundary fallback={<p>This section could not be loaded.</p>}>
      <ProductPanel />
    </ErrorBoundary>
  );
}

Here, ProductPanel is a function component; only the reusable boundary is a class. Replace reportErrorToService with your own reporting function. The second argument to componentDidCatch includes componentStack, which can help identify where the error occurred. React warns that a thrown value is not guaranteed to be an Error instance, so logging should not assume every value has properties such as message or stack.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The snippet leaves the boundary in its failed state. If your UI needs recovery, define a deliberate reset behavior—for example, resetting when a user retries or when a relevant route or item changes. A boundary does not automatically know which application-level action makes it safe to try rendering the subtree again.

Choose between a class and a package

Option What it provides What your team still decides
Reusable class boundary Uses React’s documented boundary lifecycle methods. Fallback UI, reset behavior, and error reporting.
react-error-boundary A package alternative named in React’s documentation. Whether its API suits your fallback and reset needs, and whether it fits your project’s dependency policy. The current package version, maintenance status, and license are not established here.

React’s Component reference documents the lifecycle approach and points to the package option. The available evidence does not establish a performance, bundle-size, support, or commercial-terms advantage for either path.

Know what an error boundary catches

A boundary handles errors thrown while React renders its descendant components, including errors deep in the child tree. It does not catch every failure associated with that UI.

Situation Does the boundary catch it?
Error during descendant rendering Yes, including errors from distant descendants.
Error in an event handler No. Handle it in the event logic.
Error in a typical asynchronous callback, such as setTimeout or requestAnimationFrame No.
Error thrown by the boundary itself No. A higher boundary would be needed to catch an error from a lower boundary.
Server-rendering error No—not as a general error-boundary catch. See the server-rendering distinction below.
Error inside the function passed to startTransition React documents this as a case an error boundary can catch; it does not mean all asynchronous errors are caught.
Thrown form action React documents that it can show the nearest boundary’s fallback.

The documented cases for transition and form-action errors appear in React’s references for useTransition and <form>. Treat these as specific behaviors, not a promise that a boundary catches arbitrary rejected promises or callback failures.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why a parent try/catch is not a substitute

Wrapping JSX in a function component with JavaScript try/catch does not catch an error thrown later as React renders a child. React performs that rendering work; the parent’s synchronous try/catch does not surround it. React’s error-boundaries lint guidance shows this pattern as invalid and recommends an error boundary instead.

Server rendering and Suspense

Do not interpret a client error boundary as a general server-rendering error handler. React documents a separate streaming-render behavior for Suspense: if a component throws on the server, the nearest Suspense fallback can be used for the server output; the client then retries, and an error boundary is shown if the component also errors on the client. That is Suspense’s server-rendering behavior, not proof that error boundaries catch server errors. See React’s Suspense reference.

Place boundaries around useful recovery experiences

Choose boundaries based on what the user can still do when a region fails. A page-level boundary can replace a whole route; a boundary around a panel or independently useful item can keep the rest of the screen available. Wrapping every tiny visual component usually adds complexity without creating a more meaningful recovery experience. React’s placement guidance is in the Component reference.

  • Use a broader boundary when the page cannot remain useful without the failed subtree.
  • Use a narrower boundary when an independent region can fail while surrounding content remains usable.
  • Give the fallback an appropriate next step, such as retrying or navigating elsewhere, and implement the reset behavior that makes that action meaningful.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

React 19 reporting changes

React 19 changed how render errors are reported: uncaught errors are reported to window.reportError, while errors caught by an error boundary are reported to console.error. createRoot and hydrateRoot also provide onUncaughtError and onCaughtError callbacks for custom reporting. These changes were documented in the React 19 upgrade guide published April 25, 2024; review telemetry integrations that relied on errors being re-thrown. See the React 19 Upgrade Guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The React versions page identified React 19.3 as the latest version on October 7, 2026. Because that pointer can change, check the React Versions page when version-specific details matter.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.