Is there any direct option to persist svelte store data so that even when the page is refreshed, data will be available.
I am not using local storage since I want the values to be reactive.
You can manually create a subscription to your store and persist the changes to localStorage and also use the potential value in localStorage as default value. @AnilSivadas Doing it on the server complicates it a bit. You could skip it on the server and just do it in the browser with a typeof window !==
Create the store All we need to do to connect to local storage is create a writable store and then set a default value based on local storage and on any change (via subscribe ) we update the local storage entry.
You can manually create a subscription to your store and persist the changes to localStorage and also use the potential value in localStorage as default value.
Example
<script> import { writable } from "svelte/store"; const store = writable(localStorage.getItem("store") || ""); store.subscribe(val => localStorage.setItem("store", val)); </script> <input bind:value={$store} />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With