Storage
Reactively stores data in the browser's storage.
Usage
<script>
import { storage } from "@sveu/browser"
const state = storage("svelte-u is", "awesome")
</script> <script>
import { storage } from "@sveu/browser"
const state = storage("svelte-u is", "awesome")
</script> svelte
Example
<script>
import { storage } from "@sveu/browser"
const state = storage("svelte-u", "awesome")
</script>
<div class="flex flex-col space-y-5 mt-20 items-center">
<div>
<label for="sveu">Svelte-U is:</label>
<input
type="text"
id="sveu"
bind:value="{$state}"
/>
</div>
<button
class="w-1/4"
on:click="{() => ($state = null)}"
>remove
</button>
</div> <script>
import { storage } from "@sveu/browser"
const state = storage("svelte-u", "awesome")
</script>
<div class="flex flex-col space-y-5 mt-20 items-center">
<div>
<label for="sveu">Svelte-U is:</label>
<input
type="text"
id="sveu"
bind:value="{$state}"
/>
</div>
<button
class="w-1/4"
on:click="{() => ($state = null)}"
>remove
</button>
</div> svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Click fold/expand code
API
Arguments
| Name | Description | Type | Required |
|---|---|---|---|
| key | The key to store the data under | string | Yes |
| fallback | The fallback value | T | No |
Options
| Name | Description | Type | Default |
|---|---|---|---|
| store | The store to use | (local, session, cookie, url | local |
| sync | Whether to sync the data | boolean | true |
| onError | The function to call on error | (error: Error) => void | console.error |
| serializer | The serializer to use | StorageSerializer<T> | depends on fallback |
TIP
- sync is only available for
local. - serializer is only available for
local,session, andcookie. If your usingurl, you can't customize the serializer b/c it's useurlQueryunder the hood.