React Refs: A Guide for Practical Use Cases

Tushar PuriTushar Puri
2 min read

React’s ref system is a powerful tool that allows direct interaction with DOM elements and child components.

Understanding React Refs:

Ref Basics: Refs in React provide a way to access and manipulate DOM elements or React components directly.

Types of Refs: There are two main types of refs in React:

  • Mutable Refs: Created using useRef, they persist across component re-renders.

  • Callback Refs: Utilize functions to manage ref assignments and updates.

Practical Applications:

Accessing DOM Elements:Use useRef to directly manipulate DOM elements, such as focusing an input field on component mount.

import React, { useEffect, useRef } from 'react';

const MyComponent = () => {
  const inputRef = useRef(null);

  useEffect(() => {
    inputRef.current.focus();
  }, []);

  return <input ref={inputRef} />;
};

Handling External Events: Refs can manage interactions outside React’s virtual DOM, like closing a dropdown on outside clicks.

import React, { useRef } from 'react';
import OutsideClickHandler from 'react-outside-click-handler';

const Dropdown = () => {
  const dropdownRef = useRef(null);

  const handleCloseDropdown = () => {
    // logic to close dropdown
  };

  return (
    <OutsideClickHandler onOutsideClick={handleCloseDropdown}>
      <div ref={dropdownRef}>
        {/* dropdown content */}
      </div>
    </OutsideClickHandler>
  );
};

Forwarding Refs:forwardRef allows passing refs through higher-order components, maintaining flexibility.

import React, { forwardRef } from 'react';

const FancyButton = forwardRef((props, ref) => (
  <button ref={ref} className="FancyButton">
    {props.children}
  </button>
));

const ParentComponent = () => {
  const ref = useRef(null);

  const handleClick = () => {
    ref.current.focus();
  };

  return (
    <>
      <FancyButton ref={ref}>Click me!</FancyButton>
      <button onClick={handleClick}>Focus FancyButton</button>
    </>
  );
};

Updating State with Refs:Using refs to manage state changes ensures that functions always have the most recent state values.

import React, { useState, useRef, useEffect } from 'react';
const Counter = () => {
  const [count, setCount] = useState(0);
  const countRef = useRef(count);
  useEffect(() => {
    countRef.current = count;
  }, [count]);
  const handleClick = () => {
    setTimeout(() => {
      alert(`Count is: ${countRef.current}`);
    }, 3000);
  };
  return (
    <div>
      <p>{count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={handleClick}>Alert Count</button>
    </div>
  );
};

Conclusion:

Mastering React refs empowers developers to handle intricate DOM interactions and optimize performance. Whether managing focus, responding to user actions, or integrating with third-party libraries, refs offer versatile solutions. By leveraging these techniques effectively, you can elevate your React applications with cleaner, more efficient code.

Implement these practices to streamline your React development workflow and unlock the full potential of component-level control.

0
Subscribe to my newsletter

Read articles from Tushar Puri directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tushar Puri
Tushar Puri