Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    ⚛React

    Components and JSX

    • Why React?
    • JSX rules
    • Props
    • Rendering lists and keys
    • Conditional rendering

    State and events

    • Handling events
    • useState: a component's memory
    • Objects and arrays in state
    • Forms and controlled inputs
    • Lifting state up

    Effects and essential hooks

    • useEffect
    • Fetching data
    • useRef
    • Context: data for the whole tree
    • useReducer

    Professional React

    • Writing your own hooks
    • Performance: memo, useMemo and useCallback
    • Suspense and error boundaries
    • What's new in React 19
    • Thinking in React
    • Project: a complete to-do app
    React

    Effects and essential hooks · Lesson 13 of 21

    useRef

    Reaching the DOM, and a remembered value that doesn't re-render.

    8 minutes2 games

    useRef returns an object { current: … } that stays the same for the component's whole life. Two uses:

    1. Reach a DOM element (focus it, measure it, scroll to it). 2. Remember a value between renders without re-rendering when it changes (like a timer id).

    React
    function App() {
      const inputRef = useRef(null)
      const clicks = useRef(0)
      const [shown, setShown] = useState(0)
    
      return (
        <div>
          <input ref={inputRef} placeholder="Search courses" />
          <button onClick={() => inputRef.current.focus()}>Focus the search</button>
          <hr />
          <button onClick={() => { clicks.current++ }}>Count silently</button>
          <button onClick={() => setShown(clicks.current)}>Show count</button>
          <p>Silent clicks shown: {shown}</p>
        </div>
      )
    }
    useStateuseRef

    Change re-renders?

    yes

    no

    Read during render?

    yes

    better not

    How you change it

    setX(v)

    ref.current = v

    Uses

    anything shown on screen

    DOM, timer ids, previous values

    Important

    Don't read or write ref.current during render (except to initialize it once). Refs belong in event handlers and effects. If a value must appear on screen, it belongs in state. A DOM ref's current is null until the element renders.

    React
    function Stopwatch() {
      const [ms, setMs] = useState(0)
      const timer = useRef(null)
    
      const start = () => {
        if (timer.current) return
        timer.current = setInterval(() => setMs((t) => t + 100), 100)
      }
      const stop = () => {
        clearInterval(timer.current)
        timer.current = null
      }
      useEffect(() => stop, [])              // stop when the component goes away
    
      return (
        <div>
          <h2>{(ms / 1000).toFixed(1)}s</h2>
          <button onClick={start}>Start</button> <button onClick={stop}>Stop</button>
        </div>
      )
    }
    
    function App() {
      return <Stopwatch />
    }

    Complete the code

    Complete it so the input is focused as soon as the page opens.

    function App() {
      const box = (null)
      useEffect(() => {
        box..focus()
      }, [])
      return <input ref={box} />
    }

    Interview question

    When would you use useRef instead of useState?

    Exercise

    A Tap button counts presses in a ref without re-rendering, and a Show button copies the count into state and displays it: Taps: N. The checker presses Tap three times, then Show.

    The page should show:
    Taps: 3
    Tap Show
    React
    function App() {
      const taps = useRef(0)
    
      return (
        <div>
          <p>Taps: 0</p>
          <button className="tap">Tap</button>
          <button className="show">Show</button>
        </div>
      )
    }

    Learn more from

    • react.dev: Referencing values with refsreact.dev
    • react.dev: Manipulating the DOM with refsreact.dev
    • react.dev: useRef referencereact.dev
    • MDN: HTMLElement.focus()developer.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousFetching dataNextContext: data for the whole tree
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.