Svelte and SvelteKit 5 · Runes (reactivity)

Share state between components

Put reactive state in a .svelte.ts file and export it. Export an object, because reassigned primitives can't be exported.

// counter.svelte.ts
export const counter = $state({ value: 0 });
export function increment() {
  counter.value++;
}

Official Svelte and SvelteKit 5 documentation

More runes (reactivity) commands

All runes (reactivity) commands · Svelte and SvelteKit 5 cheatsheet

Was this page helpful?