DEV Community

Discussion on: Synchronous State With React Hooks

Collapse
 
isaachagoel profile image
Isaac Hagoel

Just a small point about states vs. refs. Not sure how helpful in your case (need to see the JSX).
I see devs reaching out to useState for every variable they want to store (because the functional componenet runs everything from the top every time and normal variables don't persist).
The thing is, states are tied to React's rendering cycle. In other words, only use states when the desired effect of changing the value is a re-render (applies to using states within custom hooks as well).
If all you need is a variable that persists between renders but doesn't need to trigger re renders, a ref is the way to go (and as you mentioned it updates like a normal variable because it is).

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

Excellent point!