@svelterun/store
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

@svelterun/store

Persisted version of Svelte's writable store.

Installation

pnpm add @svelterun/store
yarn add @svelterun/store
npm i -D @svelterun/store

Usage

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

License

MIT © Svelte.run, Nicholas Berlette

Package Sidebar

Install

npm i @svelterun/store

Weekly Downloads

1

Version

0.2.2

License

MIT

Unpacked Size

6.63 kB

Total Files

6

Last publish

Collaborators

  • nberlette