Svelte Shared State
Several components often need the same data, such as a theme choice or a shopping cart. Svelte 5 lets you share reactive state through a regular module file. Any component that imports the module reads and updates the same values.
Reactive Modules
Runes work in files that end with .svelte.js or .svelte.ts. The extension tells the compiler to process runes inside the file. Plain .js files cannot use them.
// counter.svelte.js
export const counter = $state({ value: 0 });
export function increment() {
counter.value += 1;
}
export function reset() {
counter.value = 0;
}The file exports an object and changes its properties. Exporting a bare number and reassigning it would break the link, since importing code receives a copy of the value. Wrapping the number in an object keeps every importer connected to the same source.
Using The Shared State
<script>
import { counter, increment } from "./counter.svelte.js";
</script>
<button onclick={increment}>Add</button>
<p>Count: {counter.value}</p>A second component can import the same module and display counter.value. Both components refresh after every click.
Shared Module Diagram
counter.svelte.js
(one shared object)
^ ^ ^
| | |
Header.svelte Sidebar.svelte Footer.svelte
shows value shows value shows value
increment() runs anywhere --> all three updateUsing A Class
Classes group state, derived values, and actions in one tidy package. State and derived runes work as class fields.
// cart.svelte.js
class Cart {
items = $state([]);
total = $derived(this.items.reduce((sum, i) => sum + i.price, 0));
add(item) {
this.items.push(item);
}
clear() {
this.items = [];
}
}
export const cart = new Cart();
<script>
import { cart } from "./cart.svelte.js";
</script>
<button onclick={() => cart.add({ name: "Bag", price: 60 })}>
Add Bag
</button>
<p>{cart.items.length} items, total {cart.total}</p>Getter Functions
Another safe approach keeps the state private and exports functions that read and change it.
// theme.svelte.js
let mode = $state("light");
export function getMode() {
return mode;
}
export function toggleMode() {
mode = mode === "light" ? "dark" : "light";
}Components call getMode() inside their markup, and each call stays reactive.
Server Rendering Warning
Module-level state lives once per running server process. In SvelteKit, many visitors share that process during server rendering, so user-specific data placed in a module could leak between visitors. Keep private or per-user information in context or in data returned from load functions. Use module state for safe, browser-only conveniences.
Choosing A Sharing Method
Method Best For Watch Out For
----------------- ------------------------------- ----------------------
Props Parent to child data Deep chains of passing
Context Data for one component tree Needs a parent setup
Stores Svelte 3 and 4 projects Extra dollar syntax
Reactive modules App-wide state in Svelte 5 Server sharing riskA Layman Analogy
Think of a whiteboard in a shared office hallway. Anyone who walks past reads the current plan, and anyone with a marker updates it. Every team sees the same information immediately. Locking a whiteboard inside one room would split the plan into confusing copies.
Common Mistakes To Avoid
- Naming the file with a plain .js extension, which breaks the runes
- Exporting a reassigned primitive rather than an object or getter
- Storing per-user data in module state on the server
- Mutating shared state from many places without any naming rules
Key Points
- Files ending in .svelte.js support runes
- Objects, classes, and getters keep shared state connected
- Every importing component sees the same live values
- Avoid sharing private data through module state on servers
