Skip to content

Bug: Stale closures with useEffectEvent #34818

@OliverJAsh

Description

@OliverJAsh

React version: 19.2

Steps To Reproduce

  1. Open the reduced test case and view the console
  2. Click the "Home" link (this causes navigationType to change to REPLACE)
function App() {
  const navigationType = useNavigationType();
  // ✅ This is REPLACE after clicking the link.
  console.log('render', navigationType);

  const fn = useEffectEvent(() => {
    // ❌ But this is always POP. It's not seeing the latest value!
    console.log('effect event', navigationType);
  });
  useEffect(() => {
    setInterval(() => {
      fn();
    }, 3000);
  }, []);

  return (
    <>
      <Link to="/">Home</Link>
      <Outlet />
    </>
  );
}

// 🤷‍♂️ Removing `memo` here fixes the problem.
export default memo(App);

Link to code example:

https://stackblitz.com/edit/github-utjrgufd?file=app%2Froot.tsx

The current behavior

The Effect Event receives a stale value from navigationType (POP).

Image

The expected behavior

The Effect Event should receive the latest value from navigationType (REPLACE).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions