KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
⚛️

React

Topic Hub & Articles

React Intro

10 min

Recap Quiz

Gated

Milestone Assessment

React Getting Started

10 min

React ES6

10 min

React Render HTML

10 min

Recap Quiz

Gated

Milestone Assessment

React JSX

10 min

React Components

10 min

React Class Components

10 min

Recap Quiz

Gated

Milestone Assessment

React Props

10 min

React Events

10 min

React Conditionals

10 min

Recap Quiz

Gated

Milestone Assessment

React Lists

10 min

React Forms

10 min

React Router

10 min

Recap Quiz

Gated

Milestone Assessment

React Memo

10 min

React CSS Styling

10 min

React Sass Styling

10 min

Recap Quiz

Gated

Milestone Assessment

React Fragments

10 min

React Portals

10 min

React Profiler

10 min

Recap Quiz

Gated

Milestone Assessment

React Strict Mode

10 min

React Higher Order Components

10 min

React Context API

10 min

React Error Boundaries

10 min

What is a Hook

10 min

Recap Quiz

Gated

Milestone Assessment

useState

10 min

useEffect

10 min

useContext

10 min

Recap Quiz

Gated

Milestone Assessment

useRef

10 min

useReducer

10 min

useCallback

10 min

Recap Quiz

Gated

Milestone Assessment

useMemo

10 min

Custom Hooks

10 min

useLayoutEffect

10 min

Recap Quiz

Gated

Milestone Assessment

useImperativeHandle

10 min

useDebugValue

10 min

useDeferredValue

10 min

Recap Quiz

Gated

Milestone Assessment

useTransition

10 min

useId

10 min

Progress
0%

0 / 38 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
ReactReact Hooks
Lesson

useRef

10 min reading
Free Course

React useRef Hook: Persisting Values and Accessing DOM Nodes

useRef creates a plain mutable object { current: initialValue } that stays alive across every render of a component. Unlike useState, changing ref.current never triggers a component re-render. Developers use useRef for two jobs: reaching real browser DOM nodes (like focusing an input or scrolling a section) and storing non-visual values (like setInterval IDs or previous props).

Focusing DOM Elements and Storing Timer IDs

Attach a ref to any JSX tag using the ref={inputRef} prop. After React mounts the DOM node, inputRef.current points directly to that HTML element.

import { useRef, useState } from 'react';

export function StopwatchInput() {
  const inputRef = useRef(null);
  const timerRef = useRef(null);
  const [seconds, setSeconds] = useState(0);

  function startTimer() {
    if (timerRef.current !== null) return;
    timerRef.current = setInterval(() => {
      setSeconds((s) => s + 1);
    }, 1000);
    inputRef.current?.focus();
  }

  function stopTimer() {
    clearInterval(timerRef.current);
    timerRef.current = null;
  }

  return (
    <div>
      <input ref={inputRef} placeholder="Lap label..." />
      <p>Elapsed: {seconds}s</p>
      <button type="button" onClick={startTimer}>Start</button>
      <button type="button" onClick={stopTimer}>Stop</button>
    </div>
  );
}

useRef vs useState at a Glance

Feature useState useRef
Persists between renders Yes Yes
Triggers re-render on update Yes No
Update timing Asynchronous (next render) Synchronous (ref.current = ...)
Best suited for UI data shown on screen DOM nodes, timer IDs, previous values

Common useRef Mistakes

  • Reading or Writing ref.current During Render: Reading or mutating ref.current in the render body breaks component purity and causes unpredictable behavior in concurrent mode. Access ref.current only inside event handlers or useEffect.
  • Expecting UI Updates When Mutating a Ref: Changing countRef.current += 1 will not update JSX text on screen. Use useState for anything visible in markup.

Frequently Asked Questions

Do I still need forwardRef in React 19?

No. React 19 allows custom function components to accept ref directly as a standard prop, simplifying reusable input and button wrappers.

Why is inputRef.current null on the very first render pass?

DOM nodes do not exist until React finishes rendering and commits them to the page, so refs become populated right before useEffect runs.

Can useRef take a callback instead of an object?

Yes, JSX supports callback refs (ref={(node) => ...}), and React 19 supports returning a cleanup function directly from a callback ref.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Related Developer Tools & Topic Hub

React Topic Hub
JSON Formatter
Validate and format JSON payloads
HTML Formatter
Format JSX/HTML markup
CSS Formatter
Clean and format stylesheets
All React Articles
Deep-dive guides, tips & discussions
PrevioususeContext
Continue JourneyNext: useReducer

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum