State Management & Stores
Two ways to share state
Section titled “Two ways to share state”Component-local $state is enough until two components that aren’t parent-and-child need the same data. Then you need shared state, and Svelte gives you two answers:
- Runes in a module. Put
$statein a.svelte.js/.svelte.tsfile and export it. Any component that imports it reads and writes the same reactive value. This is the modern default in Svelte 5. - Stores. The classic Svelte primitive: an object with a
subscribemethod.writable,readable, andderivedfromsvelte/storecover most needs, and the$storesyntax auto-subscribes inside components. Still fully supported — and the underlying contract is what lets Svelte interoperate with things like RxJS.
What this module covers
Section titled “What this module covers”| Lesson | What you’ll learn |
|---|---|
| The store contract | What a “store” really is: any object with subscribe |
| Built-in stores | writable, readable, derived, and the $ prefix |
| Runes vs stores | The Svelte 5 shift, and when to use each |
| Cross-component state | Sharing runed state safely (modules, context, SSR) |
The lens
Section titled “The lens”flowchart TB q["Need to share state?"] --> local["No: local $state in the component"] q --> shared["Yes"] shared --> runes["Runes in a .svelte.js module (default)"] shared --> stores["Stores: the subscribe contract / interop / existing code"]
Svelte 5 didn’t remove stores — it added a simpler option for most cases. You’ll learn both, and more importantly, when each is the right call.