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.

Double-render in development could be opt-in

See original GitHub issue

Today React double-renders with StrictMode and ConcurrentMode to help developers detect impure renderings.

It helps with class-heavy codebases, but with function/hooks codebases, the use-case looks unclear to me.

  • it adds noise to logging.
  • it bites developers that look for a hook stability issue in their codebases before they find the relevant documentation (happened to me, and from SO/Issue tracker I am not alone).

Can we consider making the behaviour opt-in or optional at the least, esp. in ConcurrentMode where there is no alternative?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Jul 22, 2020

Frankly I really wish double render would go away in concurrent mode. It bit me again today: style components global css added twice in development.

The whole point of double-rendering is to find issues like this since they would occur in Concurrent Mode in production, too (due to ability to interrupt renders). You should report it to Styled Components. In this case, double-rendering worked as intended: you noticed a real potential issue.

tl;dr; silently introducing side effects to the dev environment is not cool.

Another way to look at this is that you need to stop considering React components as functions called at particular predictable times, a concrete predictable number of times. That’s not the right mental model. The right mental model is that they’re pure-ish functions that React may call at any time, and they should not contain side effects.

In future versions, double-rendering will not be optional, so this will be something that people will learn early on. I also think this is something our docs should specify. I don’t think printing this to the console is any more helpful than printing a whole tutorial of how React works to the console. We gotta stop somewhere.

That said, to aid debugging, the next major version will suppress console logs during the second renders.

1reaction
gaearoncommented, May 18, 2020

We’ve actually silenced the second render logs on master by overriding console.log. But this is obviously controversial. I do wonder if you’re right and we should just do this for class lifecycles. Although then it will be harder to fix issues that occur due to a render side effect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

My React Component is rendering twice because of Strict Mode
StrictMode renders components twice (on dev but not production) in order to detect any problems with your code and warn you about them ......
Read more >
React useEffect double rendering fix and why you should stop ...
React18 useEffect double renders components in development with Strict mode. Let's see why that's not a problem.
Read more >
Bug: useEffect runs twice on component mount (StrictMode ...
The useEffect callback runs twice for initial render, probably because the component renders twice. After state change the component renders ...
Read more >
Solving the React 18 Double Render problem on useEffect
React 18's useEffect hook now double renders (with the empty dependency array) which has caused a stir in the React community.
Read more >
React Components rendered twice — any way to fix this?
React will soon provide a new Concurrent Mode which will break render work into multiple parts. Pausing and resuming the work between this...
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