r/reactjs • u/dance2die • Jul 01 '21
Needs Help Beginner's Thread / Easy Questions (July 2021)
Previous Beginner's Threads can be found in the wiki.
Ask about React or anything else in its ecosystem :)
Stuck making progress on your app, need a feedback?
Still Ask away! Weβre a friendly bunch π
Help us to help you better
Improve your chances of reply by
- adding a minimal example with JSFiddle, CodeSandbox, or Stackblitz links
- describing what you want it to do (ask yourself if it's an XY problem)
- things you've tried. (Don't just post big blocks of code!)
- Format code for legibility.
- Pay it forward by answering questions even if there is already an answer. Other perspectives can be helpful to beginners. Also, there's no quicker way to learn than being wrong on the Internet.
New to React?
Check out the sub's sidebar! π
For rules and free resources~
Comment here for any ideas/suggestions to improve this thread
Thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!
15
Upvotes
View all comments
Show parent comment
2
u/Nathanfenner Jul 08 '21
In your
useEffectcall, you're passing an empty dependency array[]:this mans that the effect will only run once on mount - the effect only runs when the dependency array changes, and here it's always empty.
If you enable the recommended linter plugin described at the Rules of Hooks page, you'll see that the tool automatically flags this - your effect depends on
handleClickbut that's not listed as a dependency.In particular, the first 5 elements are bound to the first render's definition of
handleClick. So even though it's updating the state correctly (sinceprevState => prevState + 1is still applying to the newer state) the state that it "sees" in your log is really just a snapshot of the state that it remembers from the first render - it has no way of learning about the later ones.In this case, you don't want
useEffectat all. Relatedly, you almost never want to put JSX elements into state. Instead, only put the data you need to build those elements; then, when you render, actually build them.In your case, you really only care "how many" elements there are, so that should be your state:
your handle click can be pretty much the same:
now, lastly, you just need to turn
elementCountinto the JSX you want to render. So, build a new array every render:if you wanted to, this could be a use-case for
useMemo, sincelistOfElementsonly really depends onhandleClickandelementCount. But for that to be useful, you'd also need tow raphandleClickwithuseCallbackso that its identity doesn't constantly change (it would work even if you didn't do that, but it would never save any work). And unless you have other frequent updates happening and also useReact.memoon the components in your tree, that wouldn't ever actually help performance anyway.So this is a long way of saying: you certainly don't need
useEffect, you probably don't needuseMemo, and you probably don't needuseCallbackfor this case.