Skip to content

effect

Reactive side effects that automatically run when dependencies change.

/**
* @param fn - The function to execute when dependencies change
* @returns A cleanup function
*/
effect(fn: EffectFn): EffectFn

Effects return a function to manually cleanup and release resources.

Like signals, cleanup is required for global effects only. Effects created inside a component function are automatically cleaned when the component is unmounted.

import { signal, effect } from '@hellajs/core';
const count = signal(0);
// Creates an effect that runs when count changes
const cleanup = effect(() => {
console.log(`The count is: ${count()}`);
});
// Logs: "The count is: 0" (initial run)
count.set(1);
// Logs: "The count is: 1"
// Stop the effect from running
cleanup();
// No longer logs anything
count.set(2);