Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.9 — Separating Events from Effects: useEffectEvent
React · Example 1 of 3

The problem: a theme change reconnects the chat

Correct but wasteful: changing a colour drops and re-opens the network connection. In a real chat, users would see "reconnecting…" every time they toggled dark mode.

Some logic inside an effect should see the latest props and state without making the effect re-run when they change; useEffectEvent (React 19.2) wraps that logic in an Effect Event, a non-reactive function you call only from inside effects.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

An effect starts an interval that adds step to count every second, with deps [step], so changing step restarts the timer. Rewrite it so changing step doesn't restart the interval but the next tick uses the new step.

02

A useEffect with [productId] logs "viewed", productId, currency. Changing currency shouldn't log a new view. Write it.

Explain it without notes

01

What problem does useEffectEvent solve? Give an example.

02

What is the stale value problem, and why does silencing the linter cause it?

03

List the rules for using Effect Events.

04

How is an Effect Event different from a function wrapped in useCallback?

The problem: a theme change reconnects the chat

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

connect to #general
toast: connected! (light theme)
--- toggle theme (same room!)
disconnect from #general
connect to #general
toast: connected! (dark theme)