-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Bug: ErrorBoundary remounts children when errors are caught #26259
Comments
I am experiencing this with componentDidUpdate, as well. It gets even weirder, because the ErrorBoundary doesn't receive all of the thrown errors. My own example: https://codesandbox.io/s/bold-brook-63i6vm?file=/src/App.js |
@fatton139 the reason yours is being remounted is because you're using StrictMode. That causes every component to mount twice. |
In your example the overlay catches the second and fourth instance for me, the third is handled by the error boundary so only Errors 0 has vanished for me. Could React internally be doing some sort of batching?
Not sure if I've done it correctly, but I removed |
Hi @fatton139
|
Thanks for the reply @Werter12 I would've expected something like the |
I think there's a case for not resetting state. An error boundary that just renders children anyway is like rethrowing an error. But there's also a case for resetting state: It likely recovers the UI. Either way, this should be documented if the current behavior is intended. @fatton139 could you check if error boundaries always behaved that way by checking if the behavior is the same in React 16? |
Hi @eps1lon, I checked on React 16.14.0 the behavior seems to be the same. Here is the playground: https://codesandbox.io/s/recursing-stonebraker-ki0yz3?file=/src/App.jsx Regardless of how we proceed, the documentation of the intended behavior would be good. Our use case is for not resetting the state, our child components throws various errors, some are fatal where the child cannot be rendered, others are non fatal where there is sufficient information to render the child + error message. The current solution is to throw errors if the error is fatal, otherwise manually set a state, ideally we'd like to abstract away the "set a state" part. |
Have exactly the same problem. In a small app I thought it would be convenient to handle errors in one place, in ErrorBoundary, and just throw everything there. Bumped into the fact that ErrorBoundary remounts children. I wanted to show little toasts, but instead all my state is gone and my mounting effects are relaunched. Super unintuitive behaviour. The interface implies that I deal with an error myself and ErrorBoundary just catches it for me. Remounting children is too big of a thing to not mention it. |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
This is intended behavior. We're resetting state to attempt to recover from the error. The idea being that the error might be based on the state being invalid/corrupted. Otherwise we'd enter an infinite loop since a re-render with the same state/props would produce the same result which is throwing an error in this case. |
In the
render
block of aErrorBoundary
component theprops.children
is remounted whengetDerivedStateFromError
derives a new state (orcomponentDidCatch
sets a state).Is this a special case ? Setting the state in any other manner (e.g
componentDidUpdate
doesn't have this behavior). I don't see this behavior documented in the React docs.Thanks!
React version: 18.2.0 (Happens on 17.0.2 as well).
Steps To Reproduce
Link to code example: https://codesandbox.io/s/nifty-platform-wub9wn?file=/src/App.tsx
The current behavior
props.children
unmounts and mounts.The expected behavior
props.children
rerenders.The text was updated successfully, but these errors were encountered: