DEV Community

Discussion on: Five common mistakes writing react components (with hooks) in 2020

Collapse
 
rubenmeseguer7 profile image
Ruben Meseguer

Is there any downside to doing it this way? Looks clean :)

Collapse
 
marlo22 profile image
marcin93 • Edited

I use this regularly and I've never faced problems. :) I treat this a little like a "class property in functional components".

But of course there are cases where this approach can not work properly. It's depend from an individual case.

Thread Thread
 
valeriydyachenko profile image
Valerii Diachenko

If there are several components, for example, several counters on a page, the disadvantage is that several components will refer to one variable. In the case of useRef, each component will have its own instance of this variable.

Thread Thread
 
loweisz profile image
Lorenz Weiß

That's true ! 🤔 So storing it outside the component can actually be quite dangerous.
Thank you for that ! :)

Thread Thread
 
marlo22 profile image
marcin93 • Edited

Yes, that's the case, Valerii. When component has a few instances some problems can occur. But when we use this approach in a single-instance component like page view for example, everything should be fine.