cdellacqua / svelte-persistent-writable

A Svelte writable store that persists data. It supports local and session storage out-of-the-box and can easily be extended to target different storage mechanisms

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

svelte-persistent-writable

A Svelte writable store that persists data. It supports local and session storage out-of-the-box and can easily be extended to target different storage mechanisms

NPM Package

npm install svelte-persistent-writable

Full documentation:

Working demo:

You can clone this repo and run npm run dev to see it working

Highlights

Create a writable store that persists its value using the localStorage

const myWritable = persistentWritable(0, {
	storage: localStorageAdapter('myWritable')
});

Create a writable store that persists its value using the sessionStorage

const myWritable = persistentWritable(0, {
	storage: sessionStorageAdapter('myWritable')
});

In a Svelte component, the created PersistentWritable can be used just like any other Writable store:

Here is the value:
<div>{$myWritable}</div>

About

A Svelte writable store that persists data. It supports local and session storage out-of-the-box and can easily be extended to target different storage mechanisms

License:MIT License


Languages

Language:TypeScript 73.3%Language:JavaScript 19.4%Language:Svelte 4.4%Language:HTML 2.9%