Skip to content

About

A Svelte store that persists to localStorage

Topics

Resources

Stars

1.2k stars

Watchers

11 watching

Forks

Latest commit

 

History

343 Commits

Folders and files

Repository files navigation

npm version license codecov

svelte-persisted-store

A Svelte store that persists to local storage. Can sync changes across browser tabs.

Used by

Installation

npm install svelte-persisted-store

Usage

Define the store:

import { persisted } from 'svelte-persisted-store'

// First param `preferences` is the local storage key.
// Second param is the initial value.
export const preferences = persisted('preferences', {
  theme: 'dark',
  pane: '50%',
  ...
})

Then, to use it:

import { get } from 'svelte/store'
import { preferences } from './stores'

preferences.subscribe(...) // subscribe to changes
preferences.update(...) // update value
preferences.set(...) // set value
preferences.reset() // reset to initial value
get(preferences) // read value
$preferences // read value with automatic subscription

Additional options can be specified:

import * as devalue from 'devalue'

// third parameter is options
export const preferences = persisted('local-storage-key', 'default-value', {
  serializer: devalue, // defaults to `JSON`
  storage: 'session', // 'session' for sessionStorage, defaults to 'local'
  syncTabs: true, // choose whether to sync localStorage across tabs, default is true
  onWriteError: (error) => {/* handle or rethrow */}, // Defaults to console.error with the error object
  onParseError: (raw, error) => {/* handle or rethrow */}, // Defaults to console.error with the error object
  beforeRead: (value) => {/* change value after serialization but before setting store to return value*/},
  beforeWrite: (value) => {/* change value after writing to store, but before writing return value to local storage*/},
})

License

MIT

About

A Svelte store that persists to localStorage

Topics

Resources

Stars

1.2k stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages