Skip to content

docs: add enhanced ErrorBoundary fallback example for React 19 #33975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

DEVBALIYAN21
Copy link

πŸ“¦ Pull Request: Add Enhanced ErrorBoundary Example and Docs for React 19

🧠 Summary

This PR addresses a user-reported issue β€” #33967 β€” where class-based error boundaries in React 19 silently fail to render fallback UI even though componentDidCatch() is triggered and errors are logged.

After updating to React 19, many teams discovered unusual production behavior:

  • componentDidCatch correctly logs errors
    -❗ No fallback UI is rendered
  • The sibling component tree continues to function
  • Users experience no visible indication that an error occurred

This regression does not occur in React 18 and previous versions.

What Changed?

  • This PR contributes a new example under examples/error-boundary-react19/
  • It showcases a production-robust implementation of ErrorBoundary.jsx
  • Includes:
    • Minimal failing BuggyComponent.jsx
    • Recovery functionality (reset button)
    • Developer-only componentStack rendering in dev mode
    • Comments on integrating Sentry/LogRocket

🚧 Problem This Solves (from #33967)

After deploying my application using React 19 to production, I intermittently receive error notifications triggered by my custom error boundary (i.e., componentDidCatch in a class-based ErrorBoundary component).

Behavioral Flow:

  1. Deploy a React 19 app
  2. Use it as normal (in production)
  3. Error occurs (confirmed via monitoring)
  4. No broken UI or visual fallback appears
  5. Tree continues to behave as if nothing is wrong

Expected:

  • When an error is caught by an ErrorBoundary, the fallback UI should trigger immediately.

Actual:

  • No fallback UI is rendered.
  • Errors are only logged (Sentry, DevTools).
  • No user-facing disruption.
  • Only happens in React 19+ builds.

βœ… What's Included in This PR

πŸ“‚ examples/error-boundary-react19/

  • ErrorBoundary.jsx: Robust, production-safe implementation
  • BuggyComponent.jsx: Simulates an error at runtime
  • App.jsx: Demonstrates fallback UI and logging
  • README.md: Explains root cause, React 19 change, and usage pattern

πŸ§ͺ Testing Instructions

Manually tested via:

  • yarn install
  • yarn build && yarn preview
  • Confirmed:
    • Fallback is rendered only if getDerivedStateFromError is defined βœ…
    • Logging in componentDidCatch remains intact βœ…
    • Fallback UI is skipped when getDerivedStateFromError is missing ❌

🧩 Advantages Over Current Docs

Feature React Docs Example This PR Example βœ…
Requires getDerivedStateFromError βœ… Basic βœ… Yes
Fallback UI w/ Reset Button ❌ None βœ… Yes
Dev-only error stack trace ❌ None βœ… Yes
Real bug reproduction ❌ None βœ… Yes
Production-tested demo ❌ No builds βœ… Vite + CLI test
Monitoring integration hints ❌ No βœ… Yes (Sentry-ready)
Addresses migration confusion ❌ Implicit only βœ… Directly

βœ… Final Thoughts

  • Resolves #33967
  • Helps teams avoid silent production failures
  • Teaches devs why getDerivedStateFromError is critical under React 19+
  • Improves DX and confidence in error-handling strategy for apps running in Live environments

πŸ—³ CLA Signed

βœ… I confirm that I have signed the Meta Contributor License Agreement (CLA)

@facebook-github-bot
Copy link
Contributor

Hi @DEVBALIYAN21!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@gaearon gaearon closed this Jul 25, 2025
@facebook facebook locked as spam and limited conversation to collaborators Jul 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Exception thrown by Error Boundary after production build in React 19, not visible in UI but reported via monitoring
3 participants