concept current in category react

appears as: current
React Hooks in Action

This is an excerpt from Manning's book React Hooks in Action.

The useRef function returns an object with a current property, as shown in figure 6.4. Every time React runs the component code, each call to useRef will return the same ref object for that call.

Figure 6.4 useRef returns an object with a current property.

The first time React invokes the component code, it assigns the initial value you pass the useRef function to the ref object’s current property.

const ref1 = useRef("Towel");
const ref2 = useRef(42);
 
ref1.current;  // "Towel"
ref2.current;  // 42

On subsequent renders, React assigns the same ref objects to the respective variables, based on the order of the useRef calls. You can persist state values by assigning them to the current properties of the refs:

ref1.current = "Babel Fish";
ref2.current = "1,000,000,000,000";

Assigning new values to the current properties of the ref objects doesn’t trigger a re-render. But, as React always returns the same ref objects, the new values are available when the component runs again.