|
1 |
| -<script lang="ts"> |
2 |
| - import Button from '$lib/ui/Button.svelte'; |
3 |
| - |
4 |
| - import { createQueryParamStore } from '$lib/stores/queryParam'; |
5 |
| - const item = createQueryParamStore({ key: 'item', log: true, replaceState: true, persist: 'localStorage', 'startWith': 'foo' }); |
6 |
| - |
7 |
| - const object = { |
8 |
| - foo: 'hi there', |
9 |
| - bar: { |
10 |
| - blah: 123, |
11 |
| - quux: [1, 2, 3], |
12 |
| - }, |
13 |
| - }; |
14 |
| -</script> |
15 |
| - |
16 | 1 | # Query Param Store
|
17 | 2 |
|
18 |
| -Update by changing URL: |
19 |
| -<Button href="/stores/0-query-param">No query param</Button> |
20 |
| -<Button href="/stores/0-query-param?item=">no value</Button> |
21 |
| -<Button href="/stores/0-query-param?item=123">nav to 123</Button> |
22 |
| -<Button href="/stores/0-query-param?item=1234">nav to 1234</Button> |
23 |
| - |
24 |
| -Update by setting store value: |
25 |
| -<Button onclick={() => ($item = '')}>Set to empty string</Button> |
26 |
| -<Button onclick={() => ($item = 12345)}>Set to 12345</Button> |
27 |
| -<Button onclick={() => ($item = 123456)}>Set to 123456</Button> |
28 |
| -<Button onclick={() => ($item = 0)}>Set to 0</Button> |
29 |
| -<Button onclick={() => ($item = object)}>Set to object</Button> |
30 |
| -<Button onclick={() => ($item = [1, 'hi', object])}>Set to array w/ object</Button> |
31 |
| -<Button onclick={() => ($item = 'Hello world? & some = that')}>Set to String with characters needing escaped</Button> |
32 |
| -<Button onclick={() => ($item = null)}>Set to null</Button> |
33 |
| -<Button onclick={() => ($item = undefined)}>Set to undefined</Button> |
34 |
| -<Button onclick={() => ($item = false)}>Set to false</Button> |
35 |
| -<Button onclick={() => item.remove()}>Remove function</Button> |
36 |
| - |
37 |
| -<pre>{JSON.stringify($item, null, 2)}</pre> |
38 |
| - |
39 |
| -<!-- prettier-ignore --> |
40 | 3 | - server/client side query params based store
|
41 | 4 | - Must be initialized from within a `.svelte` file and not a `.ts` file (see https://github.com/sveltejs/kit/issues/2381) because only components can access the `$page` store. Listening to the URL won't work because we want this to work server-side as well.
|
42 | 5 | - Only works with SvelteKit as it relies on kit imports. Feel to copy the code and create your own that works with other Svelte frameworks. If needed, refer to the inspiration repos for this tool: [svelte-pathfinder](https://github.com/sveltetools/svelte-pathfinder) and [svelte-store-router](https://github.com/zyxd/svelte-store-router)
|
|
0 commit comments