Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2
Svelte store that saves and loads data from localStorage or sessionStorage
License
furudean/svelte-persistent-store
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is awritable svelte storethat saves and loads data fromWindow.localStorage orWindow.sessionStorage.Works with Svelte Kit out of the box.
The store listens to events from theStorage interface and will sync itsinternal state upon changes. This makes debugging using the developer consoleeasy, and it will update across sessions as well.
npm install @furudean/svelte-persistent-store
Note: By default onlyJSON serializable valuesare handled, butcustom serialization and deserialization functions can beprovided.
import{persistent}from"@furudean/svelte-persistent-store"constpreferences=persistent({start_value:{foo:"bar"},key:"preferences",// key to save as in Storagestorage_type:"localStorage"// Storage object to use})
Since theStorage interface only supports strings, data needs to be convertedto strings before saving. By defaultJSON.stringify andJSON.parse is used.
You can pass custom serializer and deserializer functions if you requirespecific behavior when loading or saving data fromStorage. For example, youcan handleDates like this:
constpersistent_date=persistent({start_value:newDate(),key:"my-persistent-date",storage_type:"localStorage",serialize:(date)=>date.toISOString(),// transform before savingdeserialize:(str)=>newDate(str)// transform after loading})
About
Svelte store that saves and loads data from localStorage or sessionStorage
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.
