Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readReactJavaScript

Fix stale closures in intervals with refs

Let a React interval read the latest state through a ref without resetting the timer on each edit.

You wrote an autosave that runs every second. You typed hello into the field, and the log keeps saying the text is empty. The input shows the right value, but the interval callback does not.

import { useEffect, useState } from 'react'

function Draft() {
  const [text, setText] = useState('')

  useEffect(() => {
    const id = setInterval(() => {
      console.log('saving:', JSON.stringify(text))
    }, 1000)
    return () => clearInterval(id)
  }, [])

  return <input value={text} onChange={(e) => setText(e.target.value)} />
}

After typing hello and waiting two seconds, the console prints this.

saving: ""
saving: ""

React sets up the effect after the component mounts. Its empty dependency list means edits leave the existing interval in place, and that callback still reads the initial empty string. In development Strict Mode, React runs an extra setup and cleanup cycle to check the effect.

Adding text as a dependency resets the timer

Add text to the dependency array and React recreates the callback when text changes. The price is that React clears the interval and starts a new one after each change. The useEffect docs describe the same reset for a counter.

I typed one character every 400 ms and the interval never fired until I stopped. A one second timer that restarts on every keystroke only fires when you pause.

If the interval only needs to set state, there is a smaller fix. Pass an updater, setCount(c => c + 1), and the effect no longer reads count. That does not help here, because the callback has to read text.

Read the latest value through a ref

A ref is one object that lives across renders. Write the newest text into ref.current, and have the interval read from it.

import { useEffect, useRef, useState } from 'react'

function Draft() {
  const [text, setText] = useState('')
  const textRef = useRef(text)

  useEffect(() => {
    textRef.current = text
  }, [text])

  useEffect(() => {
    const id = setInterval(() => {
      console.log('saving:', JSON.stringify(textRef.current))
    }, 1000)
    return () => clearInterval(id)
  }, [])

  return <input value={text} onChange={(e) => setText(e.target.value)} />
}

Same typing, same two seconds, and now the log shows saving: "hello" twice.

Edits update the ref without restarting the timer. The callback reads the value written by the latest effect.

I update the ref inside an effect. The React docs say not to write ref.current during rendering. Changing a ref does not trigger a render.

Update, November 2026. React 19.2 added useEffectEvent. For a timer set up inside an effect, call an Effect Event from the timer to read the latest committed state without mirroring it into a ref.