[]previous and next page

withScope

Runs fn with scope in effect, so the keys fn draws come from it.

Runs fn with scope in effect, so the keys fn draws come from it. The previous scope is restored when fn returns or throws.

Import#

ts
import { withScope } from "@shadow-js/web/extend";

Signature#

ts
function withScope<T>(scope: HydrationScope | null, fn: () => T): T

Parameters#

Name Description
scope The scope to put in effect; null runs fn with no scope (no keys are drawn).
fn The code to run, synchronously, once.

Returns#

What fn returns. An error fn throws propagates.

Examples#

ts
import { childScope, nextKey, withScope } from "@shadow-js/web/extend";

// The keys of an isolated subtree, independent of its siblings.
const first = withScope(childScope("0-4"), () => nextKey()); // "0-4-0"