question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: When throwing an error on component render, the component rerenders and throws twice, but componentDidCatch runs only once.

See original GitHub issue

This 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

  1. Test this codesandbox
  2. Click on the number until it throws the error
  3. Look at the console and see the it logs twice the message rendering BuggyCounter. count: 5
  4. Go in the package.json and change the react version from 16.12.0 to 16.0.0.
  5. 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

0reactions
stale[bot]commented, May 25, 2020

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found