| | |

Svelte 46 ๐Ÿงก Svelte 4 Legacy Stores (writable, readable, derived) Interoperability

โฑ๏ธ Estimated read: 7 min | |

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

StrategySyntaxBest For
fromStorefromStore(s)Modern Rune integration
Legacy ${$store}Quick prototyping/legacy code
Manual Substore.subscribe()Custom logic/teardown

Migration Path

Store TypeRune 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

PitfallWhy It HappensFix
Reactivity lostUsing $store inside $ blockUse fromStore
ComplexityMixing $ and RunesChoose one and stick to it
Race conditionsStores updating during renderUse 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

ItemValue
Primary BridgefromStore
CompatibilityFull (Legacy stores work)
Syntax$store (Legacy)
Future StateMigration 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.
  • fromStore is the Rune choice. Use this function to convert legacy stores into reactive runes that behave consistently with $state and $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.js modules 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.

Was this helpful?


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!