Function: effect

index.effect

effect(fn, name?): DisposeFn

Creates and runs a reactive side-effect function immediately, re-running it whenever its dependencies mutate.

Parameters

Name Type Description
fn EffectFn The reactive effect function to execute and re-run upon dependency change.
name? string Optional diagnostic name for debugging, telemetry, and observability tracking.

Returns

DisposeFn

A DisposeFn that unbinds all reactive subscriptions and cleans up resources.

Remarks

When effect is invoked, the supplied callback function runs synchronously on the current tick to capture its initial reactive dependency graph. If any tracked signal, collection, or reactive proxy property is mutated subsequently, the effect is scheduled for re-execution in a batched microtask.

If the effect callback returns a cleanup function, it will be invoked immediately before the next execution and when the effect is explicitly disposed via the returned DisposeFn.

Example

import { signal, effect } from '@banksia/signals';

const count = signal(0);
const dispose = effect(() => {
  console.log('Count changed:', count.value);

  return () => {
    console.log('Cleaning up previous count run');
  };
});

count.value += 1;
// Later, dispose when no longer needed:
dispose();
ON THIS PAGE