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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more