# 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

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

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

<ModalContent />
```

## Parameters

| Parameter | Type     | Description                                     |
| --------- | -------- | ----------------------------------------------- |
| `scopeId` | `string` | Optional identifier. Auto-generated if omitted. |

## Returns

The scoped `Store` instance.

## Initializing state

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

```svelte
<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

- [Scoped Stores](https://valdres.dev/guides/scoped-stores) — full guide on scoped stores
- [setValdresContext](https://valdres.dev/svelte/setValdresContext) — provide a store to your component tree
