JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
PrevNext

Learn the concept

Hooks

react
mid
hooks

What is useRef and when should you use it?

useRef
hooks
dom
mutable-values
refs
Quick Answer

useRef creates a mutable reference that persists across renders without causing re-renders when changed. Use it for accessing DOM elements, storing mutable values that don't need to trigger updates, and keeping references to timers or previous values.

Detailed Explanation

What useRef Does:

  • Returns a mutable ref object with .current property
  • Value persists across re-renders
  • Changing .current doesn't trigger re-render
  • Same ref object is returned every render

Use Cases:

  1. DOM Access: Focus inputs, measure elements, scroll
  2. Mutable Values: Timers, intervals, previous values
  3. Instance Variables: Values that shouldn't trigger render
  4. External Libraries: Integration with non-React code
  5. React 19 ref-as-prop: ref can be passed as a regular prop to function components — forwardRef is no longer needed

useRef vs useState:

  • useState: Value changes trigger re-render
  • useRef: Value changes don't trigger re-render

useRef vs createRef:

  • useRef: Same object across renders (hooks)
  • createRef: New object each render (class components)

React 19 Changes:

  • ref is now a regular prop for function components (no more forwardRef)
  • Callback refs can return a cleanup function (like useEffect cleanup)

Code Examples

DOM element accessJSX
function TextInput() {
  const inputRef = useRef(null);
  
  const focusInput = () => {
    inputRef.current.focus();
  };
  
  const selectAll = () => {
    inputRef.current.select();
  };
  
  return (
    <div>
      <input ref={inputRef} type="text" />
      <button onClick={focusInput}>Focus</button>
      <button onClick={selectAll}>Select All</button>
    </div>
  );
}

// Scrolling to element
function ScrollToTop() {
  const topRef = useRef(null);
  
  const scrollToTop = () => {
    topRef.current.scrollIntoView({ behavior: 'smooth' });
  };
  
  return (
    <div>
      <div ref={topRef} />
      {/* ... lots of content ... */}
      <button onClick={scrollToTop}>Back to Top</button>
    </div>
  );
}

Real-World Applications

Use Cases

Video Player Controls

Using refs to imperatively control HTML5 video elements (play, pause, seek, volume)

Canvas Drawing

Accessing canvas elements via refs for 2D/3D rendering with requestAnimationFrame

Third-Party Library Integration

Initializing non-React libraries (D3, Chart.js, map SDKs) on DOM elements

Mini Projects

Focus Management System

beginner

Build a multi-step form with automatic focus management using refs

Intersection Observer Hook

intermediate

Create a useIntersectionObserver hook using refs for lazy loading and infinite scroll

Industry Examples

CodeMirror

Uses refs to integrate the CodeMirror editor with React lifecycle

React Player

Wraps HTML5 media elements with ref-based imperative controls

Resources

React Docs - useRef

docs

React Docs - Manipulating the DOM with Refs

docs

Related Questions

What are custom hooks and how do you create them?

mid
hooks
Previous
What are custom hooks and how do you create them?
Next
What is the difference between controlled and uncontrolled components in React forms?
PrevNext