DEV Community

Franck Binde
Franck Binde

Posted on

Need help with useRef in React !

Can someone explain me how to use UseRef in React? What it is and what it does? I have tried to find tutorial online but I still can't understand that stuff. Thank you so much !

Top comments (4)

Collapse
 
jeevankishore profile image
Jeevan Kishore

There are two use cases of useRef :

  1. Retain data across multiple rerender of the component

  2. Get reference to the DOM element / tag within the component

The above values are accessed using .current of the value.

Collapse
 
fralainbk profile image
Franck Binde

Thank you for that ! Much appreciated !

Collapse
 
dgiulian profile image
Diego Giuliani

The short answer is that you can use useRef when you want to persist some value in multiple renders of your component (similar to useState) but you don't want to re-render that component when that value changes.
Tyler McGinnis has one of the best explanation on how to use useRef

Collapse
 
fralainbk profile image
Franck Binde

Thank you so much! That link you've shared is very useful.