Utility

Persisted Store

An extended version of the Svelte writable store that automatically persists to local storage

typescript
import { localStorageStore } from '@skeletonlabs/skeleton';
Source Page Source

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');

Operates as 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.