Skip to content

Instantly share code, notes, and snippets.

@Aurumaker72
Created June 28, 2023 19:28
Show Gist options
  • Select an option

  • Save Aurumaker72/c0a3bd78c717ff09a2418e7b1f94f45f to your computer and use it in GitHub Desktop.

Select an option

Save Aurumaker72/c0a3bd78c717ff09a2418e7b1f94f45f to your computer and use it in GitHub Desktop.
Svelte LocalStorageStore
import type {Writable} from "svelte/store";
import {writable} from "svelte/store";
export function lsWritable<T>(path: string, default_value: T): Writable<T> {
const store = writable<T>(localStorage.getItem(path) ? JSON.parse(localStorage.getItem(path)) as T : default_value);
store.subscribe(x => {
localStorage.setItem(path, JSON.stringify(x));
});
return store;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment