iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📚

Do Not Include useEffectEvent in the Dependency Array of useEffect

に公開
2025/11/01

Introduction

This content has been added to the following material.

https://speakerdeck.com/maguroalternative/react19-dot-2nouseeffecteventwozhui-u

I've written this article with self-reflection because I posted incorrect information. If there are any misinterpretations in this article, I would appreciate your feedback.

https://twitter.com/sigumataityouda/status/1979173036151464031

React 19.2 Released

On October 1, 2025, React 19.2 was released.

https://react.dev/blog/2025/10/01/react-19-2

Among the new features, a new hook called useEffectEvent was added, which addresses the "stale closure problem."

Stale Closure Problem

Simply put, this refers to "continuing to reference old state." For example, consider the following code:

export function Example() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    console.log("add listener")
    const handler = () => {
      console.log(count); // ← This becomes the "old value"
    };
    window.addEventListener('click', handler);
    return () => window.removeEventListener('click', handler);
  }, []);
  
  return <button onClick={() => setCount(c => c + 1)}>+1</button>;
}

The intention is to "display count in the console each time it is clicked." Clicking the button increments count. At first glance, it seems fine, but when you check the console...

Yes. count remains 0. This is because count inside useEffect continues to reference the value at the time the event handler was registered. This state is called the "stale closure problem."

A temporary solution is to add count to the dependency array of useEffect, but this introduces another problem.

export function Example() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    console.log("add listener")
    const handler = () => {
      console.log(count); // ← This becomes the "old value"
    };
    window.addEventListener('click', handler);
    return () => window.removeEventListener('click', handler);
  }, [count]);
  
  return <button onClick={() => setCount(c => c + 1)}>+1</button>;
}

Checking the console:

Now count is updated, but we see "add listener" displayed every time. This means the event listener is re-registered each time count is updated. While the count inside becomes the latest value each time, re-registering the event listener solely for that purpose is unnecessary.

We want to reference the latest count without re-registering the event listener every time... This is exactly where useEffectEvent shines.

export function Example() {
  const [count, setCount] = useState(0);

  const handlerClick = useEffectEvent(() => {
    console.log(count);
  })

  useEffect(() => {
    console.log("add listener")
    window.addEventListener('click', handlerClick);
    return () => window.removeEventListener('click', handlerClick);
  }, []);
  
  return <button onClick={() => setCount(c => c + 1)}>+1</button>;
}

We define a new handler using useEffectEvent. The difference from before is that the dependency array of useEffect is now empty. With no dependencies, the event listener is registered only on initial render.

This would normally mean count remains unchanged, but...?

Yes, it is updated. That's right: useEffectEvent always retrieves the latest state.

Should useEffectEvent Be Excluded from the Dependency Array?

Some of you may feel a sense of discomfort looking at this code. The function created with useEffectEvent is not included in the dependency array of useEffect. In fact, the linter[1] even warns about it.

So, you might think it's correct to include it in the dependency array as follows...

useEffect(() => {
  console.log("add listener useEffectEvent")
  window.addEventListener("click", handleClick);
  return () => window.removeEventListener("click", handleClick);
}, [handleClick]);

Yes, surprisingly, the event listener gets re-registered! This also means that handleClick returns a different function each time, potentially causing an infinite loop.

Why Does This Happen?

Details can be found in the following PR:

https://github.com/facebook/react/pull/25473

Since useEffect is responsible for synchronizing with external systems, it is assumed that values and functions used inside it are included in the dependency array. The linter warns about this to prevent stale closure problems. However, useEffectEvent is a hook that guarantees to hold the latest state. Doesn't it seem odd to include it in the dependency array when it always provides the latest state? Therefore, it seems that useEffectEvent was intentionally implemented to return a different function each time.

Additional Details

It seems that initially, the implementation did not return a new function every time. However, in that case, the following situation could occur.

  • With dependency array
  const handleClick = useEffectEvent(() => {
    console.log("Latest count:", count);
  });

  useEffect(() => {
    console.log("add listener useEffectEvent")
    window.addEventListener("click", handleClick);
    return () => window.removeEventListener("click", handleClick);
  }, [handleClick]);
  • Without dependency array
  const handleClick = useEffectEvent(() => {
    console.log("Latest count:", count);
  });

  useEffect(() => {
    console.log("add listener useEffectEvent")
    window.addEventListener("click", handleClick);
    return () => window.removeEventListener("click", handleClick);
  }, []);

These two pieces of code would behave exactly the same way. This makes it ambiguous whether the useEffectEvent function is an external system or not, which is not desirable in React's philosophy. So, by deliberately returning a new function each time, the implementation indicates that it is not an external system.

Summary

For now, just remember these points:

  • The useEffectEvent function should not be included in the dependency array of useEffect (because it is not an external system of useEffect).
  • Don't forget to update your linter...
    • I said I was fooled by the linter, but it's simply my fault for not updating the linter...

https://twitter.com/sigumataityouda/status/1979469752784593164

脚注
  1. The latest linter does not seem to output this warning. ↩︎

GitHubで編集を提案

Discussion