Bug: When throwing an error on component render, the component rerenders and throws twice, but componentDidCatch runs only once.
See original GitHub issueThis question on stack overflow was made and while trying to solve it, I realized something.
I found out that on version 16.12.0
of react, when you throw an error on the render method, it render and throws the error twice, but componentDidCatch
runs only once.
I version 16.0.0
it throws the error only once.
React version:
16.0.0
throws the error only once.
16.12.0
throws the error twice .
Steps To Reproduce
- Test this codesandbox
- Click on the number until it throws the error
- Look at the console and see the it logs twice the message
rendering BuggyCounter. count: 5
- Go in the
package.json
and change the react version from16.12.0
to16.0.0
. - Do that same from steps 2 and 3 and you will see the message only once.
The current behavior
The error is being thrown twice and componentDidCatch
is running only once.
The expected behavior
The error should be thrown only once or componentDidCatch
should run the number of times an error is thrown.
I’m not sure if it’s intended to be or if it’s a new thing that changed in the newer version, if that is the case, please let me know.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Why is this component being rendered after throwing an error?
So depending of your version, it will render twice or only once. Edit: Why the component rerenders? No Idea. But, the error page...
Read more >React Components rendered twice — any way to fix this?
In most cases it will throw an error message into the browser console log. It fails to automatically detect render side effects as...
Read more >Error Boundaries - React
A class component becomes an error boundary if it defines either (or both) of the lifecycle methods static getDerivedStateFromError() or componentDidCatch() .
Read more >My React components render twice and drive me crazy
Many frontend developers who use modern React, wonder why their components render twice during development but this is actually happening ...
Read more >Handling Errors in React Native: A Complete Guide
The error boundaries API only works with class Component, and a class component becomes an error boundary if you define one of these ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is an intentional behavior in DEV. https://github.com/facebook/react/blob/c55c34e46a6d8148afb78594d14f4675f9346900/packages/shared/ReactFeatureFlags.js#L16-L18
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!