DEV Community

Cover image for Demystifying React Hooks: useRef

Demystifying React Hooks: useRef

Milu on June 20, 2020

React Hooks changed the game when they came out! They are a simpler way to encapsulate stateful behavior and side effects in a user interface while...
Collapse
 
davidyaonz profile image
David Yao

I love how you explain things in such a clear way. Thanks!

Collapse
 
milu_franz profile image
Milu

Thanks David! :)

Collapse
 
caroso1222 profile image
Carlos Roso

Great illustrations and awesome explanation. Thanks, Keep it up!

Collapse
 
milu_franz profile image
Milu

Thank you for reading and the words of encouragement Carlos!

Collapse
 
ponyjackal profile image
ponyjackal

Milu, I love this article

Collapse
 
milu_franz profile image
Milu

Thank you!!! :)

Collapse
 
valainisgt profile image
Greg Valainis

Great article on the underpinnings and usage of useRef. Your examples and illustrations do a great job of showing practical usage. However, your discussion about createRef is somewhat misleading. createRef "creates a new instance of the ref on every render instead of keeping a value between renders" is only true within a function component. createRef should not be used in function components. It should be used in class components where a new instance is not created on every render.

Collapse
 
milu_franz profile image
Milu

Thank you for pointing this out Greg! I haven't used class components since React hooks came out and I shouldn't have assumed everybody is on the same boat as me. I've updated my post to clarify the use of createRef. Thanks again for the feedback!

Collapse
 
diazevedoo profile image
Diego Azevedo

Illustrations, pictures, clear examples. Valuable content. Thanks so much for sharing that! It is my new favorite series about Hooks.

Collapse
 
milu_franz profile image
Milu

Thank you Diego! I really appreciate your feedback :)

Collapse
 
jacobmgevans profile image
Jacob Evans

This was awesome! I'll pass it along to some learners I know! Great work!!

Collapse
 
milu_franz profile image
Milu

Thank you for the kind words and for sharing this post Jacob!

Collapse
 
terkwood profile image
Felix Terkhorn

Yeah!!! Besides being great info on React, this is the level of quality we should all be striving for in our technical articles! 🔥

Collapse
 
milu_franz profile image
Milu

Thank you Felix! This comment encourages me to continue sharing content :)

Collapse
 
travelintervals profile image
Euphoric

Thanks for this article and the codepen examples!
One question, in your "Where should you update a useRef() value?" example, you have testRef = testRef.current + 1. Can it just be testRef.current + 1? I believe testRef is a plain object, so there's no need to set testRef equal to a number; it's enough to update the current property.

Collapse
 
dnguyen profile image
Duy Nguyen

Thank you so much, great explanation!

Collapse
 
monfernape profile image
Usman Khalil

I always struggled with hooks other than useState and useEffect. Thank you for explaining it elegantly

Collapse
 
milu_franz profile image
Milu

Thank you Usman! :)

Collapse
 
emmanuelkaranja profile image
emmanuel-karanja

Great work! Crystal clear explanation!

Collapse
 
milu_franz profile image
Milu

Thank you! :)

Collapse
 
biomathcode profile image
Pratik sharma

Best article that i could find on useRef. Great work Milu.

Collapse
 
vipinkumarsn4 profile image
Vipin Saini

Great post, learnt a lot.
Thanks Milu.

Collapse
 
milu_franz profile image
Milu

Thanks Vipin!

Collapse
 
peteregbujie profile image
Peter Egbujie

5 star content.