Skip to content

@jsvision/ui / runWithOwner

Function: runWithOwner()

runWithOwner<T>(owner, fn): T

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

Run fn with owner as the active scope, then restore the previous scope. Re-entrant and nestable. Unlike createRoot, this opens no new scope — it re-parents creation onto an existing owner you already hold, so any effect/computed/nested-createRoot created inside fn attaches to owner and is disposed with it.

This is the tool for attaching new reactive work to a lifetime you don't currently sit inside — e.g. building a child's effects under its parent's scope so the child is torn down with the parent, even though the child was constructed outside any scope.

It sets the owner only, not a tracking context: reads inside fn do not subscribe anything; an effect created inside still tracks normally when it runs. With owner === null, created computations are unowned and dev-warn.

Type Parameters

T

T

Parameters

owner

Owner | null

The scope to make active for the duration of fn (or null for unowned).

fn

() => T

The function to run.

Returns

T

Whatever fn returns.

Example

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

const scope = createRoot((_dispose) => getOwner()); // capture a scope to reuse later
const label = signal('hi');
// ...elsewhere, attach a new effect to that captured scope:
runWithOwner(scope, () => {
  effect(() => render(label())); // runs now, and is disposed when `scope` is disposed
});