effect()
Run a side effect that re-executes when its signal dependencies change.
Signature
Section titled “Signature”function effect(fn: () => void | (() => void)): () => voidParameters
Section titled “Parameters”| Param | Type | Description |
|---|---|---|
fn | () => void | (() => void) | Effect function. Can return a cleanup function. |
Returns
Section titled “Returns”() => void — dispose function that stops the effect.
Examples
Section titled “Examples”import { signal, effect } from "@whisq/core";
const count = signal(0);
const dispose = effect(() => { console.log(`Count: ${count.value}`);});// Logs: "Count: 0"
count.value = 1; // Logs: "Count: 1"dispose(); // Stop watchingcount.value = 2; // No logWith cleanup
Section titled “With cleanup”effect(() => { const timer = setInterval(() => tick(), 1000); return () => clearInterval(timer);});