DEV Community

zehra hindioğlu
zehra hindioğlu

Posted on

2

What is React useRef()

useRef is a structure that allows us to keep a "mutable" variable within a component without triggering the component to be rendered again.

Image description

If we had performed the above example with useState, after clicking the button once, the component would be rendered again and "Clicks: 1" would appear on the screen. Since we keep the variable with useRef, the first value "Clicks: 0" will appear, but the "count" value will be 1 in memory.

What is the difference between useRef and useState?
When a variable changes, useState triggers the component to be rendered again. useRef does not trigger the component to be rendered again.

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay