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

    Function computed

    • Define a read-only signal computed from the values of other signals. Dependencies are tracked automatically: whatever signals fn reads during evaluation become its dependencies, re-tracked on every evaluation (so conditional reads narrow or widen them dynamically).

      Evaluation is lazy and memoized: fn does not run until the computed is first read, and re-runs only when a read (or an effect/subscription) finds a dependency actually changed. A re-evaluation that produces an equal value (strict equality) stops propagation — dependents do not re-evaluate.

      fn must be pure and synchronous: writing to a signal during a computed evaluation throws a TypeError. Computed signals cannot be written to; calling one with an argument throws a TypeError.

      Type Parameters

      • T

      Parameters

      • fn: () => T

      Returns ReadonlySignal<T>

      const a = signal(1);
      const b = signal(2);
      const sum = computed(() => a() + b());
      sum(); // 3 — evaluated on this first read
      a(2); // nothing evaluated yet
      sum(); // 4 — evaluated now