Explanation of useRef Hooks in React js!

Nadim Chowdhury - Nov 5 '23 - - Dev Community

The useRef hook in React is a built-in hook that provides a way to create and manage references to DOM elements and other values in functional components. It is often used to interact with the DOM or to store mutable data that doesn't trigger component re-renders when it changes. Here's how it works:

  1. Creating a Ref: To create a ref, you can use the useRef hook and assign it to a variable. For example:
   import React, { useRef } from 'react';

   function MyComponent() {
     const myRef = useRef();
     // You can also initialize the ref with an initial value:
     // const myRef = useRef(initialValue);

     // ...
   }
Enter fullscreen mode Exit fullscreen mode

Now, myRef is a reference object that you can use to access and manipulate DOM elements or other values.

  1. Attaching Refs to DOM Elements: You can attach a ref to a DOM element by using the ref attribute in your JSX. For example:
   function MyComponent() {
     const myRef = useRef();

     const handleClick = () => {
       // Access the DOM element using the ref
       myRef.current.focus();
     }

     return (
       <div>
         <input ref={myRef} />
         <button onClick={handleClick}>Focus Input</button>
       </div>
     );
   }
Enter fullscreen mode Exit fullscreen mode

In this example, when the button is clicked, it uses the myRef to focus on the input element.

  1. Accessing and Manipulating Refs: You can access and manipulate the value of a ref using the current property of the ref object. For example:
   const value = myRef.current;
   myRef.current = newValue;
Enter fullscreen mode Exit fullscreen mode

It's important to note that when you change the current property of a ref, it won't trigger a re-render of your component.

  1. Storing Other Values: useRef is not limited to just DOM elements; you can use it to store any mutable value. This value persists across re-renders and does not trigger component updates. For example:
   function MyComponent() {
     const count = useRef(0);

     const increment = () => {
       count.current += 1;
       console.log('Count:', count.current);
     }

     return (
       <div>
         <button onClick={increment}>Increment</button>
       </div>
     );
   }
Enter fullscreen mode Exit fullscreen mode

In this example, count is a ref that stores a mutable value (an integer) and persists across re-renders without causing the component to update.

useRef is a versatile hook that is commonly used for managing DOM elements, creating timers, storing previous values for comparison, and other scenarios where you need to maintain mutable state without causing re-renders.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player