Hook · Experimental
useEffectEvent
Non-reactive logic inside effects
useEffectEvent extracts logic that must run inside an effect but should not re-trigger it when it changes. It breaks the tension between 'I need to read this value' and 'I don't want the effect to depend on it'.
The problem
An effect that opens a connection shouldn't re-open it every time the log message changes — but if you use the log inside the effect, React requires you to include it in the dependencies, causing unnecessary reconnections.
The solution
Wrap the non-reactive logic in a useEffectEvent. The resulting function can be called from the effect without being one of its dependencies. Still experimental in React 19: import as experimental_useEffectEvent.
Common pitfalls
- 01useEffectEvent is still experimental in React 19 and is not in the stable API.
- 02The event function cannot be used as a dependency — it's not reactive.
- 03Do not use it as an escape hatch to avoid adding real dependencies that should be there.
Was this helpful?
Sign in to give feedback
