effect()
effect(run: () => void | (() => void)): DisposeRuns code that affects the outside world in response to state changes.
Description
Runs the passed function immediately once, then re-runs it whenever any state it read changes. Use it for work that reaches outside the screen — server calls, storage writes, timers, or external subscriptions.
Parameters
run— The function to execute. If it returns a cleanup function, that cleanup is called right before the next run and at disposal time.
Return Value
A dispose function that stops the effect. Effects created inside a component are automatically disposed when that component is removed from the screen, so you'll rarely need to call this yourself.
Example
const stop = effect(() => {
document.title = `${unread()} unread`;
});Caution
Writing back to state that the effect itself reads creates a cycle. In development builds, a warning is logged if the same effect runs repeatedly within a single tick.
