Skip to main content

Command Palette

Search for a command to run...

๐Ÿ“˜ React Mastery Series โ€” Day 23

Updated
โ€ข3 min readโ€ขView as Markdown
S

Frontend Developer | React & JavaScript | Building production-ready web apps Developer with 1.5+ years of experience, currently focused on React, Redux, and full-stack development. Sharing practical tutorials, concepts, and project learnings.

Ref Forwarding & Advanced useRef Patterns in React

By now, we know:

๐Ÿ‘‰ useRef can access DOM
๐Ÿ‘‰ It can store mutable values
๐Ÿ‘‰ It doesnโ€™t cause re-render

But today we go one level deeper.

We learn:

โœ” Ref Forwarding
โœ” Advanced real-world useRef patterns


๐Ÿค” Problem: Refs Donโ€™t Automatically Pass to Custom Components

Example:

function CustomInput() {
  return <input type="text" />;
}

function App() {
  const inputRef = useRef(null);

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

This wonโ€™t work.

Why?

Because refs only work directly on DOM elements โ€”

not normal functional components.


โœ… Solution โ€” Ref Forwarding

React provides:

๐Ÿ‘‰ React.forwardRef


๐Ÿ›  Example โ€” Forwarding Ref Properly

import React, { useRef } from "react";

const CustomInput = React.forwardRef((props, ref) => {
  return <input type="text" ref={ref} />;
});

function App() {
  const inputRef = useRef(null);

  const handleFocus = () => {
    inputRef.current.focus();
  };

  return (
    <div>
      <CustomInput ref={inputRef} />
      <button onClick={handleFocus}>Focus Input</button>
    </div>
  );
}

Now it works ๐ŸŽฏ


๐Ÿง  What is Ref Forwarding?

Ref Forwarding allows a parent component to pass a ref through a child component to a DOM element.

Very useful in:

โœ” Reusable UI libraries

โœ” Custom input components

โœ” Form components

โœ” Modal components


๐Ÿ”ฅ Advanced useRef Pattern 1 โ€” Storing Timers

import { useRef } from "react";

function TimerExample() {
  const timerRef = useRef(null);

  const startTimer = () => {
    timerRef.current = setTimeout(() => {
      alert("Time's up!");
    }, 2000);
  };

  const stopTimer = () => {
    clearTimeout(timerRef.current);
  };

  return (
    <div>
      <button onClick={startTimer}>Start</button>
      <button onClick={stopTimer}>Stop</button>
    </div>
  );
}

โœ” Useful for cleanup

โœ” Prevent memory leaks


๐Ÿ”ฅ Advanced Pattern 2 โ€” Prevent Multiple Clicks

function ButtonExample() {
  const clickedRef = useRef(false);

  const handleClick = () => {
    if (clickedRef.current) return;

    clickedRef.current = true;
    console.log("Clicked once!");
  };

  return <button onClick={handleClick}>Click Me</button>;
}

โœ” Stores state without re-render

โœ” Useful in API calls


๐Ÿง  When Should You Use Ref Forwarding?

โœ” Building reusable component libraries

โœ” Creating custom form inputs

โœ” Integrating third-party libraries

โœ” Managing focus manually


โŒ When NOT to Use It

โŒ For normal prop passing

โŒ As state replacement

โŒ Without real need


๐Ÿง  Interview One-Liner

Ref forwarding allows a parent component to pass a ref to a child componentโ€™s DOM element using React.forwardRef.


๐Ÿ”— How It Connects

โœ” useRef (Day 17)

โœ” Event Handling (Day 22)

โœ” Controlled Forms (Day 18)

โœ” Reusable Components

This is intermediate-to-advanced React knowledge.


โœ… Key Takeaways

โœ” Refs donโ€™t automatically pass to custom components

โœ” React.forwardRef solves it

โœ” useRef can store timers & flags

โœ” Useful in real-world component design


๐Ÿ”œ Coming Next

Day 24 โ€” Error Boundaries in React (Handle UI Crashes Gracefully)

Now we enter production-level thinking ๐Ÿ”ฅ

More from this blog

React Mastery Series

29 posts