scope

Signature
scope(scopeId?: string): Store

valdres-svelte Create an isolated child store in a Svelte component

Creates a scoped store that inherits state from the parent but can override values independently. Sets the scoped store as the context for child components.

Usage

<script>
  import { scope } from "valdres-svelte"

  const scopedStore = scope("modal")
</script>

<ModalContent />

Parameters

ParameterTypeDescription
scopeIdstringOptional identifier. Auto-generated if omitted.

Returns

The scoped Store instance.

Initializing state

Since scope() returns the store directly, initialize values after creating it:

<script>
  import { scope } from "valdres-svelte"
  import { nameAtom, emailAtom } from "./store"

  const scopedStore = scope("edit-form")
  scopedStore.set(nameAtom, "Draft")
  scopedStore.set(emailAtom, "")
</script>

<EditForm />

Cleanup

The scope is automatically cleaned up when the component is destroyed.

See also