react / react/react

Bug: `useEffectEvent` retain the first render value when is used inside a component wrapped in `memo()`

Open
#35,187 8 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Status: Unconfirmed
Dominant language
JavaScript
Stars
251k
Forks
51.4k
Avg merge
2d 4h
Merged PRs (30d)
53

Description

useEffectEvent retains the first render value when it's used inside a component wrapped in memo()

React version: 19.2.0

Steps To Reproduce

Minimal repro:

import { useState, useEffectEvent, useEffect, memo } from "react";

function App() {
  const [counter, setCounter] = useState(0);

  const hello = useEffectEvent(() => {
    console.log(counter);
  });

  useEffect(() => {
    const id = setInterval(() => {
      hello();
    }, 1000);
    return () => clearInterval(id);
  }, []);

  return (
    <div>
      <button onClick={() => setCounter(counter + 1)}>{counter}</button>
    </div>
  );
}

export default memo(App);

Link to code example:
https://codesandbox.io/p/sandbox/gj356q

The current behavior

If you click the button and increment the counter you always see 0 in the log. By removing the memo you will see the current counter value.

The expected behavior

See the latest counter value even if the component is wrapped in memo, since the documentation https://react.dev/reference/react/useEffectEvent does not describe any different behavior if component is wrapped in memo.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked CodeSandbox and minimal reproduction using React 19.2.0, then compare the memo-wrapped and unwrapped cases while observing the interval log. The issue is resolved when the memo-wrapped component logs the latest counter value rather than retaining 0.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.