[]previous and next page

Common mistakes

Common mistakes in the Effects and lifecycle guide.

For agentsUpdated
  • Doing browser work in the component body. The body also runs on the server. Put window, document and timers in onMount or useEffect.
  • Creating effects after an await. The owner is gone. Capture it with getOwner() and use runWithOwner.
  • Expecting an effect to run synchronously. useEffect is queued. Use useRenderEffect if you need the first run immediately, or flushSync() 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 useMemo or a derived constant.
  • Forgetting cleanup. Anything an effect starts (listeners, timers, subscriptions) needs a returned function or onCleanup.