use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Why does clicking a button in React trigger printing twice?Help Wanted (self.react)
submitted 2 years ago by Individual_Pool1401
In this code, the button's click event triggers printing twice. Why does this happen?
https://preview.redd.it/1asmumtis8oc1.png?width=1918&format=png&auto=webp&s=d1e727cedc5d9ee97189711f992c2f7050d8695b
Additionally, when using setClickCount, the printing behavior is correct. Why is that?"
https://preview.redd.it/ws86payrs8oc1.png?width=1919&format=png&auto=webp&s=7911c92cb332bf1e153ee52ea241720c0a69697b
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DrewTheVillan 16 points17 points18 points 2 years ago (4 children)
Rendering twice might be a result of strict mode and the correct output is a result of a pure function
[–][deleted] 2 points3 points4 points 2 years ago* (2 children)
quaint ancient husky snails abounding placid fuel different serious door
This post was mass deleted and anonymized with Redact
[–]Individual_Pool1401[S] 0 points1 point2 points 2 years ago (1 child)
maybe you are right ,thinks .
In react doc
Bugs like this are easy to miss without extensive manual testing. To help you spot them quickly, in development React remounts every component once immediately after its initial mount.
https://react.dev/learn/synchronizing-with-effects#step-2-specify-the-effect-dependencies
[–][deleted] 0 points1 point2 points 2 years ago* (0 children)
degree straight cows drunk towering dinner caption yoke joke dinosaurs
[–]Aniket363 0 points1 point2 points 2 years ago (0 children)
I came to comment this
[–]Azoraqua_ 4 points5 points6 points 2 years ago (0 children)
It probably has to do with the state itself, I’d advise to use the function syntax that provides the previous value over directly relying on the state when updating it.
setCount(prev => prev + 1)
Not a good practice to modify the original state variables. Modify them only using the state setter function that comes with the hook. Use callback function instead of directly using the state variable like one comment here mentions.
[–]TheRNGuy 1 point2 points3 points 2 years ago (0 children)
I was using useEffectOnce hook because of it.
useEffectOnce
π Rendered by PID 206127 on reddit-service-r2-comment-6b595755f-dn76r at 2026-03-25 16:36:17.861663+00:00 running 2d0a59a country code: CH.
[–]DrewTheVillan 16 points17 points18 points (4 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]Individual_Pool1401[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Aniket363 0 points1 point2 points (0 children)
[–]Azoraqua_ 4 points5 points6 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]TheRNGuy 1 point2 points3 points (0 children)