useEffectEvent

React chapter 15 · React 19.2 · MUI useEventCallback · source: react.dev/reference/react/useEffectEvent

The problem

An effect that runs a timer needs the newest count, but listing count as a dependency restarts the timer on every change. Leaving it out keeps a stale count.

The pattern that fixes it

useEffectEvent wraps the part that only needs to read the latest values. The effect depends on delay alone, so the timer keeps running and every tick sees the current count. MUI shipped the same idea earlier as useEventCallback.

In plain words

An effect is code that keeps something running, like a timer. Sometimes that code needs to peek at the newest value without being restarted every time the value changes. useEffectEvent lets you mark exactly that peeking part.

A security guard patrols on a fixed schedule. When the manager changes the visitor list, the guard should read the new list at the next stop, not restart the whole patrol from the front door.

Terms

effect
Code React runs after rendering to start something outside React, like a timer or a connection.
dependency list
The array at the end of useEffect. When any value in it changes, React stops the old effect and starts a new one.
stale value
An old copy of a variable captured by a function that was made on an earlier render.
effect event
A function made with useEffectEvent that always sees the latest values but never counts as a dependency.

Going deeper

How does it work?

Every render creates new functions that remember the values from that render. An effect keeps the function from the render it started in, which is why it goes stale. useEffectEvent gives you a function whose body is swapped to the newest render after each commit, while its identity stays out of the dependency rules. Calling it inside the interval always runs the latest body.

Pitfalls: where it does nothing, or is not allowed

Only call an effect event from inside effects (or other effect events). Do not pass it to children or put it in a dependency array; the linter will complain. It also does not replace dependencies that really should restart the effect: if the timer should restart when delay changes, delay stays in the list.

When not to use it, and alternatives

If the code is a normal click handler, you do not need it, handlers already see the latest render. If a value should restart the effect (a room id, a URL), keep it as a dependency. Before React 19.2 people stored the function in a ref, which is what MUI useEventCallback does. The React Compiler does not remove the need for this split.

Keep learning

Overview

Time until next tick Add 1 every 0.7s, five times Every tick, the timer reports the count it can see. Times timer was (re)started Restarts that were needed Ticks that arrived Ticks that saw an old count Waiting for the first tick… Press Add 1 to count, then wait for a tick. delayChanges ? : 'Press Add 1 to count a few times and watch the progress bar jump back to empty.' : ticks === 0 ? 'Press Add 1 to count and wait for a tick.' : stale === 0 && restarts Unexpected result, please reload. The timer was set up once and never learned about the new count. Each new count tears the timer down and builds a new one. A busy page can keep it from ever ticking. Real life: an auto-save timer that stamps the current number of edits. It must keep its rhythm and also report the latest edit count. Which workaround to try Leave count out of the list Put count in the list count missing: the timer keeps the first one count listed: timer restarts on every change Leave it out The timer never restarts, but each tick shows a count from the past. List it Ticks are fresh, but the timer starts over whenever the count changes. In Leave count out, press Add 1 twice and wait for a tick. It says it saw 0. Switch to Put count in the list and press Add 1 every 0.7s with a 2s timer. The bar keeps resetting and ticks never arrive. Same auto-save timer. Switch between the three versions and compare the bars. Which version of the effect Count left out Count in list always the newest count only the timer speed restarts it Use useEffectEvent when An effect must read a value but should not restart because of it. Examples: logging, a toast with the current theme, analytics. Keep it a dependency when The effect should truly start over (new room, new URL, new speed). You only need it in a click handler. Handlers already see the latest value. Keep useEffectEvent. Press Add 1 every 0.7s. The progress bar fills and ticks keep arriving with the real count. Press Change tick speed. The timer restarts once. That is correct, because speed is a real dependency.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.