@svelterun/store
Persisted version of Svelte's writable
store.
pnpm add @svelterun/store
yarn add @svelterun/store
npm i -D @svelterun/store
./stores.js
import { writable } from '@svelterun/store'
/**
* @param {string} key - localStorage key
* @param {*} value - the store's initial value
* @returns {import('svelte/store').Writable}
*/
export const preferences = writable('preferences', {
theme: 'dark',
pane: '50%',
// ...
})
./App.svelte
import { get } from 'svelte/store'
import { preferences } from './stores'
// subscribe to changes
preferences.subscribe(value => console.log('preferences:\n', value))
// update value
preferences.update(current => ({...current, theme: 'light'}))
// set value
preferences.set(value)
// read value
get(preferences)
// read value with auto subscription
$preferences
MIT © Svelte.run, Nicholas Berlette