Common mistakes
Common mistakes in the Effects and lifecycle guide.
- Doing browser work in the component body. The body also runs on the server. Put
window,documentand timers inonMountoruseEffect. - Creating effects after an
await. The owner is gone. Capture it withgetOwner()and userunWithOwner. - Expecting an effect to run synchronously.
useEffectis queued. UseuseRenderEffectif you need the first run immediately, orflushSync()to run the queue. - Writing the signal an effect depends on, unconditionally. Each run re-triggers itself and ends with
INFINITE_LOOP. Guard the write, or use the two-argument form so the read is not tracked. - Deriving with an effect. An effect that copies one signal into another costs a second pass. Use
useMemoor a derived constant. - Forgetting cleanup. Anything an effect starts (listeners, timers, subscriptions) needs a returned function or
onCleanup.