DEV Community

Cover image for Five common mistakes writing react components (with hooks) in 2020

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

Lorenz Weiß on May 25, 2020

This post was originally posted here React as a framework React has been out in the world of web development for quite some time now an...
Collapse
 
marlo22 profile image
marcin93 • Edited

Great tips, thank you. :) When comes to using useRef hook to store variable value between rerenders, I prefer just keep variable out of the component's scope. For me it's more clear and clean.

let variable = 'Hello';

const Component = () => {
  return <span>{variable}</span>
};
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.

Collapse
 
andrewslobodianiuk profile image
Andrew Slobodianiuk

Thank you for the great article! Could you please clarify, is it valid to wrap 'button' into HTML tag 'a' ? As I know HTML validator will show an error. Maybe if we want to add a link element that looks like a button we should customize 'a' tag?

Collapse
 
pabloabc profile image
Pablo Berganza

Ideally yes. You'd have to style the anchor tag to look like a button. It's considered bad practice to have an interactive element inside of another (a.k.a button inside an anchor, anchor inside another anchor, etc).

Collapse
 
jmojico profile image
Julian Mojico

Brief, good explanation, beautiful writing and great selection of the common mistakes.

Also liked your final conclusion:
"But making mistakes is also important when learning a framework or programming language and probably nobody is 100% free of these mistakes."

Thanks for sharing.

Collapse
 
theluk profile image
Lukas Klinzing

Problem with 3 is that it will setstate even though the component might be unmounted already. So you need to handle unsubscriptions. But that you have to do anyways

Collapse
 
codepumps profile image
Serkan Sayhan

Awesome, I am a student that try to learn React. That's very important for me. When I code Hooks, I will be careful.

Collapse
 
kethmars profile image
kethmars

That's a really nice article Thank you!

Collapse
 
loweisz profile image
Lorenz Weiß

Thanks 😄 Glad you liked it!

Collapse
 
starpebble profile image
starpebble

Thanks for helping us forge paths with react hooks! This is a very nice post.

Collapse
 
juanpacebe profile image
Juan • Edited

Thanks, I learned a lot 👏