Skip to content

effect()

Run a side effect that re-executes when its signal dependencies change.

function effect(fn: () => void | (() => void)): () => void
ParamTypeDescription
fn() => void | (() => void)Effect function. Can return a cleanup function.

() => void — dispose function that stops the effect.

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 watching
count.value = 2; // No log
effect(() => {
const timer = setInterval(() => tick(), 1000);
return () => clearInterval(timer);
});