svelte-society / recipes-mvp

this repo has been archived - pls head to https://github.com/svelte-society/sveltesociety.dev/tree/master/src/pages/recipes to contribute recipes!
https://github.com/svelte-society/sveltesociety.dev/tree/master/src/pages/recipes
MIT License
289 stars 18 forks source link

Components client side storage #33

Closed babycourageous closed 4 years ago

babycourageous commented 4 years ago

Adds Client Side Storage recipe

dasDaniel commented 4 years ago

I've also found a simple way to do a localStorage that uses a watch to update the store

      <script>
        let lStore = {};
        let storeKey = "MY_STORE";

        try {
          lstore = JSON.parse(localStorage.getItem(storeKey));
        } catch (e) {}

        $: if (lStore) {
          localStorage.setItem(storeKey, JSON.stringify(lStore));
        }

        lStore.name = lStore.name || 'world';
      </script>

      <input bind:value="{lStore.name}" />
      <h1>Hello {lStore.name}!</h1>
swyxio commented 4 years ago

thanks @dasDaniel, i will add that in