Double-render in development could be opt-in
See original GitHub issueToday 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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >
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
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.
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.
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.