javascript / expert
Snippet
Legacy Store Interoperability: Wrapping Writable Stores
During incremental migrations from Svelte 4 to 5, you may need to access legacy stores. This pattern wraps a 'writable' store in a Svelte 5 rune-compatible object, exposing the value through reactive getters and setters.
snippet.js
1
2
3
4
5
6
7
8
function fromStore(store) {let value = $state(undefined);store.subscribe(v => value = v);return {get current() { return value; },set current(v) { store.set(v); }};}
svelte
Breakdown
1
let value = $state(undefined);
Creates a local reactive rune to mirror the store's value.
2
store.subscribe(v => value = v);
Synchronizes the store's state with the rune.
3
get current() { return value; }
Provides reactive read access in Svelte 5 components.