runWithOwner
Edit this pagerunWithOwner executes a function under the provided owner.
Import
import { runWithOwner } from "solid-js";Type
type Owner = unknown;
function runWithOwner<T>(owner: Owner | null, fn: () => T): T | undefined;Parameters
owner
- Type:
Owner | null - Required: Yes
Owner used while executing fn.
fn
- Type:
() => T - Required: Yes
Function executed under owner.
Return value
- Type:
T | undefined
Returns the value produced by fn, or undefined when an error is routed through Solid's error handling.
Behavior
- During the synchronous execution of
fn,runWithOwnerrestores the provided owner for cleanup, context lookup, and descendant computations created insidefn. runWithOwnerdoes not restore dependency tracking because the current tracking listener is cleared whilefnruns.- Code after the first
awaitin anasyncfunction runs without the restored owner or reactive dependency tracking.
Examples
Restore an owner in a callback
import { createEffect, getOwner, runWithOwner } from "solid-js";
function Example() { const owner = getOwner();
queueMicrotask(() => { if (owner) { runWithOwner(owner, () => { createEffect(() => { console.log("effect created under the captured owner"); }); }); } });
return null;}