Svelte 46 ๐งก Svelte 4 Legacy Stores (writable, readable, derived) Interoperability
The Svelte 5 reactivity system, based on Runes, offers a more powerful and intuitive way to manage state. However, Svelte 4 stores are still widely used in existing codebases and third-party libraries. To support these legacy systems, Svelte 5 provides a clear path for interoperability. You don’t need to rewrite your entire store-based logic at once; instead, you can bridge the gap between reactive stores and Svelte 5 Runes, allowing you to use legacy state within modern components.
This chapter covers how to consume Svelte 4 stores in a Svelte 5 context using the fromStore rune, how to bridge store values into $state properties, and the strategies for migrating from legacy patterns to Runes.
Key point: The fromStore rune is the primary bridge in Svelte 5; it takes an existing legacy store and exposes its value as a reactive Rune property, allowing you to treat store data as if it were defined via $state.
Why store interoperability exists
The migration runway.
Large applications cannot be rewritten in a day. Interoperability allows you to adopt Svelte 5 and Runes incrementally, converting one component or module at a time while keeping the legacy data layer intact.
The third-party library ecosystem.
Many Svelte 4-era libraries (like router libraries or state management plugins) export legacy stores. Interoperability ensures these libraries remain functional in Svelte 5 projects.
Avoiding “store-hell.”
The $: reactivity and .subscribe() patterns in legacy Svelte often created complex dependency chains. By bridging stores into Runes, you can use modern $derived and $effect mechanics to simplify legacy data flows.
Unifying the data layer.
Eventually, you want your app to use Runes. The interop layer provides the “glue” that lets you treat stores as reactive sources, effectively turning legacy stores into temporary vessels that will eventually be replaced by $state exports.
Gradual adoption without loss of reactivity.
Without interop, you would lose reactivity when accessing stores. Svelte 5 guarantees that your UI continues to update automatically even if it is reading from an old-school writable store.
a. Using fromStore for seamless bridging
The fromStore function is a rune that takes a store and returns a reactive value. This value is read-only from the perspective of the component (as stores are accessed via their own API), but the value itself is fully reactive within the Svelte 5 system.
<script>
import { fromStore } from 'svelte/store';
import { myLegacyStore } from './stores.js';
// Create a reactive rune version of the store
const storeValue = fromStore(myLegacyStore);
</script>
<p>Value: {$storeValue}</p> <!-- Note: fromStore returns a sync/reactive value -->
When the store’s underlying value updates, Svelte automatically notifies any components that access the rune created by fromStore. You no longer need to use the $ prefix for subscription; the rune handles the subscription management for you.
b. Converting stores to $state
If you want to migrate a legacy store into a full-fledged Svelte 5 state model, you can subscribe to the store and assign its initial value to a $state variable. This breaks the link to the store’s internal state management and moves it into the Runes reactivity system.
<script>
import { myLegacyStore } from './stores.js';
let localState = $state(myLegacyStore.get()); // If using store.get()
myLegacyStore.subscribe(val => {
localState = val;
});
</script>
This pattern is useful for “detaching” a component from a global store. Once detached, you can refactor the logic within the component to use $state and $derived without worrying about the external legacy store’s API.
c. Legacy $: and $store syntax
Svelte 5 still supports the legacy $store syntax for simple cases. If you have an existing store, you can still use {$myStore} directly in your HTML. Svelte 5 keeps this legacy support intact to ensure backwards compatibility with every feature of Svelte 4.
<script>
import { count } from './stores.js';
</script>
<!-- Legacy syntax works in Svelte 5! -->
<button on:click={() => count.update(n => n + 1)}>
Count is {$count}
</button>
While this works, it is discouraged for new code. Prefer the fromStore rune, as it provides a cleaner, more consistent reactivity model that works better with the rest of the Svelte 5 feature set, such as $derived and custom effects.
Complete Example Session
// ============================================
// PART 1: CONSUMING WITH FROMSTORE
// ============================================
<script>
import { fromStore } from 'svelte/store';
import { countStore } from './stores.js';
const count = fromStore(countStore);
</script>
<p>Count: {count}</p>
// ============================================
// PART 2: DERIVING FROM STORES
// ============================================
<script>
const double = $derived(count * 2);
</script>
<p>Double: {double}</p>
// ============================================
// PART 3: LEGACY BRIDGE
// ============================================
<button on:click={() => countStore.set(0)}>Reset</button>
The session shows consuming stores with fromStore, creating new reactive values derived from those stores, and interacting with the legacy store’s API.
Quick Reference
Interop Strategies
| Strategy | Syntax | Best For |
|---|---|---|
fromStore | fromStore(s) | Modern Rune integration |
Legacy $ | {$store} | Quick prototyping/legacy code |
| Manual Sub | store.subscribe() | Custom logic/teardown |
Migration Path
| Store Type | Rune Replacement |
|---|---|
writable | $state |
derived | $derived |
readable | $state (readonly export) |
Best Practices
โ Do This:
// Use fromStore in new components
const val = fromStore(legacyStore); // โ
// Use derived on store-based runes
const result = $derived(val * 2); // โ
// Keep store interaction in modules
// (Don't leak stores into deep component trees) // โ
โ Don’t Do This:
// Use legacy syntax mixed with Runes
// (Stick to fromStore for consistency) // โ
// Use stores if you can use Runes
// (Only use stores for interop/external libs) // โ
// Ignore the teardown of stores
// (fromStore handles it, but manual subs don't) // โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Reactivity lost | Using $store inside $ block | Use fromStore |
| Complexity | Mixing $ and Runes | Choose one and stick to it |
| Race conditions | Stores updating during render | Use reactive runes |
Real-World Examples
1. Router Store Bridge
const route = fromStore(routerStore);
2. Auth Store Bridge
const user = fromStore(authStore);
3. Legacy Theme Store
const theme = fromStore(themeStore);
4. Settings Store
const settings = fromStore(settingsStore);
5. Language/I18n Store
const lang = fromStore(i18nStore);
6. Notification Legacy Store
const notes = fromStore(notifStore);
7. Analytics Event Store
const analytics = fromStore(eventStore);
8. WebSocket Buffer
const socket = fromStore(socketStore);
9. Global Cart Store
const cart = fromStore(cartStore);
10. API Data Store
const data = fromStore(apiStore);
Visual
The Interop Bridge
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ LEGACY STORE SVELTE 5 RUNE โ
โ writable() โโโบ fromStore() โโโบ UI โ
โ โ
โ (Subscription) (Reactive Property) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Migrating Data Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Stores โโโบ Legacy Components โ
โ โ
โ Runes โโโบ Modern Components โ
โ โ
โ fromStore() โโโบ The Glue (Interoperability) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Primary Bridge | fromStore |
| Compatibility | Full (Legacy stores work) |
| Syntax | $store (Legacy) |
| Future State | Migration to Runes |
Key takeaways:
- Legacy stores are fully supported. You don’t have to break your app to use Svelte 5; existing stores continue to work as intended.
fromStoreis the Rune choice. Use this function to convert legacy stores into reactive runes that behave consistently with$stateand$derived.- Incremental migration. Migrate your stores one by one to Runes modules as you refactor component logic.
- Reduce complexity. Store interop is a temporary state; aim to phase out stores in favor of
.svelte.jsmodules for all new application state. - Clean API. Interoperability allows you to keep the simplicity of your modern Runes even when the data source is an older store structure.
Remember: Svelte 5 is designed for the real world, and the real world has plenty of legacy code. Store interop ensures that you can enjoy the speed and simplicity of Runes today while keeping your existing library and store-based infrastructure functional until you’re ready to migrate it fully.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!