๐ React Mastery Series โ Day 23
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 ๐ฅ




