useRef

useRef

The useRef hook in React provides a way to create a mutable reference that persists across component re-renders without causing a re-render when its value changes. While often associated with referencing DOM elements, useRef can also be used to store and manage references to any JavaScript object.

Example

function MyComponent() {
    const myObjectRef = useRef({ data: 'initial value', count: 0 });

    const updateObject = () => {
        myObjectRef.current.data = 'updated value';
        myObjectRef.current.count++;
        console.log(myObjectRef.current); // Logs the updated object
    };

    // ...
}