Course contentsShow
← React
Lesson 1323 of 2,39633. Error BoundariesFree lesson

ErrorBoundary Props: fallback and FallbackComponent

Using fallback for simple UI and FallbackComponent for access to error and resetErrorBoundary props.

ErrorBoundary Props: fallback and FallbackComponent

You'll learn: How to choose between fallback for simple error UI and FallbackComponent when you need access to error details and reset functionality.

Two Ways to Display Errors

The react-error-boundary library's <ErrorBoundary> component gives you two props for controlling what users see when errors occur: fallback and FallbackComponent. Think of them as "simple mode" vs "advanced mode."

Using fallback for Simple UI

The fallback prop accepts JSX directly—perfect when you just need static content:

<ErrorBoundary fallback={<div>Something went wrong</div>}>
  <RiskyComponent />
</ErrorBoundary>

This is clean and straightforward, but you don't get any information about what went wrong or any way to retry.

Using FallbackComponent for Rich UI

When you need more control, use FallbackComponent instead. This prop accepts a component that receives three useful props automatically:

function ErrorFallback({ error, resetErrorBoundary }) {
  return (
    <div role="alert">
      <p>Something went wrong:</p>
      <pre>{error.message}</pre>
      <button onClick={resetErrorBoundary}>Try again</button>
    </div>
  );
}

<ErrorBoundary FallbackComponent={ErrorFallback}>
  <RiskyComponent />
</ErrorBoundary>

The component receives:

  • error: The actual error object with message and stack
  • resetErrorBoundary: Function to clear the error and retry
  • resetKeys: Additional reset trigger props (covered previously)

When to Use Which

Use fallback for generic "oops" messages. Use FallbackComponent when you need error details for debugging, reset buttons for users, or conditional UI based on error type.

Key Takeaway: fallback is for simple static error UI, while FallbackComponent gives you a component with access to error details and resetErrorBoundary functionality—choose based on how much control you need.