Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Svelte store that saves and loads data from localStorage or sessionStorage

License

NotificationsYou must be signed in to change notification settings

furudean/svelte-persistent-store

Repository files navigation

A girl

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.

Install

npm install @furudean/svelte-persistent-store

Use

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})

Custom serialization functions

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

Stars

Watchers

Forks

Sponsor this project

    Contributors2

    •  
    •  

    [8]ページ先頭

    ©2009-2025 Movatter.jp