Skip to content
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

React 18: Error boundaries actually catches errors in effects #21712

Closed
eps1lon opened this issue Jun 21, 2021 · 2 comments · Fixed by #21723
Closed

React 18: Error boundaries actually catches errors in effects #21712

eps1lon opened this issue Jun 21, 2021 · 2 comments · Fixed by #21723
Labels
React 18 Bug reports, questions, and general feedback about React 18 Type: Discussion

Comments

@eps1lon
Copy link
Collaborator

eps1lon commented Jun 21, 2021

Given

function ThrowInEffect() {
  React.useEffect(() => {
    throw new Error('passive effect')
  }, [])
  return null;
}

In React 17 this error got logged to the console (and triggered the error overlay in create-react-app) regardless of whether ThrowInEffect was a child of an Error Boundary or not.

In React 18 (createRoot and legacy root API) an Error Boundary will prevent logging that error to the console (and not trigger the error overlay in create-react-app).

Errors during render work like they did in React 17.

Behavior in React 17: https://codesandbox.io/s/react-17-error-boundaries-vp21e?file=/src/index.js
Behavior in React 18: https://codesandbox.io/s/react-18-error-boundaries-psd56?file=/src/index.js

Recording of effect errors in React 17 and 18 and with and without Error Boundaries_

react18-error-boundaries.mp4
@eps1lon eps1lon added Type: Discussion React 18 Bug reports, questions, and general feedback about React 18 labels Jun 21, 2021
@eps1lon eps1lon changed the title Error boundaries actually catch errors in effects Error boundaries actually catches errors in effects Jun 21, 2021
@eps1lon eps1lon changed the title Error boundaries actually catches errors in effects React 18: Error boundaries actually catches errors in effects Jun 21, 2021
@eps1lon
Copy link
Collaborator Author

eps1lon commented Jun 22, 2021

Update: React 18 behaved as 17 in dbe3363 but changed in 7ec4c55. Looking at dbe3363...7ec4c55 I suspect #21666 changed the behavior. The error is no longer logged when I try the codesandbox build from #21666

@gaearon
Copy link
Collaborator

gaearon commented Jun 24, 2021

Thanks for catching this. Pun intended. Fix is in #21723.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
React 18 Bug reports, questions, and general feedback about React 18 Type: Discussion
Projects
None yet
3 participants
@gaearon @eps1lon and others