# useEffectEvent()

> The `useEffectEvent()` hook in React extracts non-reactive logic from Effects into an Effect Event.

## What does useEffectEvent do in React?

The hook that creates a function always seeing the latest props and state but not causing the Effect to re-run. Effect Events must not be listed in the dependencies of `useEffect()`.

- Topic: Hooks
- Type: hook

## Example

Wrong:

```jsx
useEffect(() => {
  const conn = connect(roomId);
  conn.on('connected', () => showToast('Connected', theme));
  return () => conn.disconnect();
}, [roomId, theme]);
```

Right:

```jsx
const onConnected = useEffectEvent(() => showToast('Connected', theme));
useEffect(() => {
  const conn = connect(roomId);
  conn.on('connected', () => onConnected());
  return () => conn.disconnect();
}, [roomId]);
```

Changing `theme` no longer reconnects to the room, and the toast still uses the current theme.

## Common mistakes

- Listing the Effect Event in the dependency array, which re-runs the Effect on every render.
- Wrapping all logic in Effect Events to dodge dependencies, hiding real stale data bugs.

## Key points

- Values read only inside the Effect Event are not in the Effect dependency array, everything else still is.
- The Effect Event is called only from inside Effects, not during render or passed to other components.
- It replaces `useRef` workarounds for reading the latest props, and lint warnings are not silenced.

## Interview questions

### useEffectEvent vs useCallback: what is the difference?

`useCallback` returns a function that changes when its dependencies change. An Effect Event always sees the latest values and is meant to be called only from Effects.

### Is useEffectEvent part of stable React?

Yes. It is imported from 'react', and the React Hooks lint rules know not to require it in dependency arrays.

## Related cards

- [useEffect()](https://wonderium.dev/react/use-effect.md)
- [Dependencies](https://wonderium.dev/react/dependencies.md)
- [Effects](https://wonderium.dev/react/effects.md)
- [useCallback()](https://wonderium.dev/react/use-callback.md)
- [useRef()](https://wonderium.dev/react/use-ref.md)

---

- Level: [React](https://wonderium.dev/react.md)
- HTML version: https://wonderium.dev/react/use-effect-event
- Last updated: 2026-11-25
