InterviewHubPRO
Section Overview
Q298

Question 298 of 445

Part 9: React Hooks Deep Dive (262 - 316)

67%
React & Modern Frontend (445 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

What are the common usecases of useRef hook?

Some of the common cases are:

  • Automatically focus an input when a component mounts.
  • Scroll to a specific element.
  • Measure element dimensions (offsetWidth, clientHeight).
  • Control video/audio playback.
  • Integrate with non-React libraries (like D3 or jQuery).

Practical Code Example

jsx
// useRef Common Use Cases:
// 1. DOM element reference (input focus)
// 2. Retaining mutable timer IDs
// 3. Storing previous state across renders
const prevCountRef = useRef();
useEffect(() => { prevCountRef.current = count; });