compute - v3.0.0-beta.0
    Preparing search index...

    Function effect

    • Run fn once immediately, tracking every signal it reads, and re-run it whenever any of those change. Dependencies are re-tracked on every run, so conditional reads narrow or widen them dynamically.

      By default re-runs are batched: all writes in the same tick coalesce into a single re-run on the next microtask, observing the final state. Pass { sync: true } to re-run synchronously at each write instead.

      An effect that writes to its own dependencies re-runs until it stabilizes. If it doesn't stabilize within a bounded number of runs, the effect is disposed and an error is thrown (for a default effect this surfaces as an uncaught exception from the microtask flush, since there is no caller to receive it).

      Effects stay registered until disposed — they are never garbage-collected on their own. Avoid creating effects inside other effects or inside computed functions: each run of the outer computation would register a fresh inner effect.

      Parameters

      Returns Subscription

      a Subscription; unsubscribe() disposes the effect.

      const a = signal(1);
      const b = signal(2);
      const dispose = effect(() => console.log(a() + b())); // logs 3
      a(2); b(3); // logs 5, once, on the next microtask
      dispose.unsubscribe();