neux - v0.19.0
    Preparing search index...

    Function effect

    • Creates a reactive effect that re-runs whenever tracked properties change.

      The getter function is executed immediately. Any property read on a signal during this execution is subscribed to the effect. When a subscribed property is written, the effect is scheduled to re-run (batched via setTimeout(…, 0): one re-run per effect, however many of its properties were written in the meantime). An effect is not re-run by its own writes made during its getter, such as the length that push() reads and then writes; a write to the same key from elsewhere in the same tick still re-runs it.

      An optional setter receives the return value of getter. When the getter returns a Promise, setter is called with its resolved value, unless the effect re-ran or was disposed in the meantime.

      An error thrown by getter on the first run propagates to the caller and leaves the effect disposed. On a later run it unsubscribes the effect permanently, and an error thrown by setter is kept from stopping the effect; both are reported through reportError (or console.error where it is unavailable) rather than thrown, so other effects of the same update still run. A promise that rejects is reported the same way, unless the effect re-ran or was disposed in the meantime, and the effect keeps running.

      The context is determined by this, following the same rules as signal.

      Parameters

      • this: void | object

        Reactive context; defaults to the shared global context.

      • getter: () => any

        Function that reads reactive properties and optionally returns a value.

      • Optionalsetter: (value: any) => void

        Optional callback that receives the return value of getter, or its resolved value when getter returns a promise.

      Returns () => void

      A disposal function that unsubscribes the effect from all tracked properties.

      // creates a reactive signal
      const state = signal({ count: 0 });
      // subscribes to changes
      const dispose = effect(() => {
      // read with subscription
      console.log(state.count);
      });
      // triggers the effect
      state.count += 1;
      // later …
      dispose();