Skip to content

@jsvision/ui / createRoot

Function: createRoot()

createRoot<T>(fn): T

Defined in: ui/src/reactive/owner.ts:73

Open a root reactive scope and run fn inside it. Every effect, computed, and nested scope created during fn (or later, by callbacks that run in this scope) belongs to this scope, and dispose() tears them all down at once. This is the top-level entry point for any reactive work that owns its own lifetime.

Type Parameters

T

T

Parameters

fn

(dispose) => T

Receives the scope's dispose callback; its return value becomes createRoot's.

Returns

T

Whatever fn returns.

Example

ts
import { createRoot, signal, effect } from '@jsvision/ui';

const stop = createRoot((dispose) => {
  const n = signal(0);
  effect(() => console.log('n =', n()));
  n.set(1); // effect re-runs
  return dispose;
});
stop(); // disposes the scope: the effect and its subscriptions are gone