Local Storage Store
UtilityAn extended version of the Svelte writable store that includes pub/sub to local storage.
Import
Package
Source
Doc
Usage
The first parameter storeExample
is the local storage key name. The second parameter is the initial value of the store.
import type { Writable } from 'svelte/store';
const storeExample: Writable<string> = localStorageStore('storeExample', 'initialValueHere');
This operates exactly like a standard Svelte writable store but with the added benefit of automatic persistence via Local Storage.
import { get } from 'svelte/store';
// Subscribe to the store
storeExample.subscribe(() => {});
// Update the value
storeExample.update(() => {});
// Set the value
storeExample.set(() => {});
// Read the value
get(storeExample);
// Read value with automatic subscription
$storeExample
Attribution
Source code provided courtesy of Joshua Nussbaum. Please consider sponsoring his work.