query-store

1.1.2 • Public • Published

query-store

query-store gives you a writable svelte store that syncs with the browser's search params. Makes for easy bookmarking and sharing of UI state.

Usage

<script>
import query from 'query-store'
</script>

<input bind:value={$query.someparam}>

demo

And that's all there is to it.

History Navigation

To create a history entry on each query change (using pushState instead of replaceState), call keepHistory with the name of the param.

<script>
import query from 'query-store'
query.keepHistory('seed')
</script>

Current seed is {$query.seed}
<button on:click={() => $query.seed = Math.random()}>Generate new seed</button>

Integrate with routers

If you use a router that has its own navigation handling (eg Sapper), you can sync the router's query params to the query store like this:

<script>
import query from 'query-store'
import { stores } from '@sapper/app'

const { page } = stores()
$: query.setWithoutHistory($page.query)
</script>

Current tab is {$query.tab}

<a href="{$page.path}?tab=general">Home</a>
<a href="{$page.path}?tab=details">Details</a>
<a href="{$page.path}?tab=edit">Edit</a>

Readme

Keywords

none

Package Sidebar

Install

npm i query-store

Weekly Downloads

1

Version

1.1.2

License

ISC

Unpacked Size

3.16 kB

Total Files

3

Last publish

Collaborators

  • buhrmi