Skip to content

State Management & Stores

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 $state in a .svelte.js / .svelte.ts file 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 subscribe method. writable, readable, and derived from svelte/store cover most needs, and the $store syntax auto-subscribes inside components. Still fully supported — and the underlying contract is what lets Svelte interoperate with things like RxJS.
LessonWhat you’ll learn
The store contractWhat a “store” really is: any object with subscribe
Built-in storeswritable, readable, derived, and the $ prefix
Runes vs storesThe Svelte 5 shift, and when to use each
Cross-component stateSharing runed state safely (modules, context, SSR)
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"]
Choosing where shared state lives

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.

When do you need shared state (beyond component-local `$state`)?
What is the modern default for shared state in Svelte 5?
Did Svelte 5 remove stores?