Local Storage Store

Utility

An extended version of the Svelte writable store that includes pub/sub to local storage.

Getting Started

The first parameter storeExample is the local storage key name. The second parameter is the initial value of the store.

typescript
import type { Writable } from 'svelte/store';
typescript
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.

typescript
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.