scope
Signature
scope(scopeId?: string, options?: ScopeOptions): Storevaldres-svelte Scoped child store for a component subtree
Creates a scoped child store layered over the parent for a subtree, and exposes it to descendants via context. Reuses an existing scope of the same scopeId if one is already in the tree; otherwise it creates one and detaches it automatically when the owning component is destroyed. Descendants reading via fromState / toStore resolve the scoped store.
Usage
<!-- CheckoutSection.svelte -->
<script lang="ts">
import { scope, fromState } from "valdres-svelte"
import { countAtom } from "$lib/state"
scope("checkout", { initialize: () => [[countAtom, 100]] })
const count = fromState(countAtom) // reads the scoped value (100)
</script>
<p>Scoped count: {count.current}</p>
The optional initialize callback seeds the scoped store inside a transaction when the scope is created — the same InitializeCallback contract used by setValdresContext. It must be called during component initialization (it reads context and registers an onDestroy cleanup).
See also
- setValdresContext — provide the root store
- getValdresContext — read the current (scoped) store