paoloricciuti / sveltekit-search-params

The easiest way to read and WRITE from query parameters in sveltekit.
https://sveltekit-search-params.netlify.app
MIT License
497 stars 15 forks source link

Added the ability to pass a defaultValue to the encode and decode object #4

Closed paoloricciuti closed 1 year ago

paoloricciuti commented 1 year ago

Added the ability to pass a defaultValue to the encode and decode object.

This will allow the user to pass a default value that will be set if, on load, the query param is not present.

<script lang="ts">
    import { queryParam } from "sveltekit-search-params";

    const count = queryParam("count", {
        encode: (value: number) => value.toString(),
        decode: (value: string | null) => value ? parseInt(value) : null,
        defaultValue: 10,
    });
</script>

The count is {$count}
<input bind:value={$count} type="number"/>

Warning

You can't run goto on the server so if the page is server side rendered it will still have the null value (this is to say don't relay on the assumption that the store will always be not null).

You can also pass a default value to the function that will be the defaultValue of the object.

<script lang="ts">
    import { ssp, queryParam } from "sveltekit-search-params";

    const count = queryParam("count", ssp.number(10));
</script>

The count is {$count}
<input bind:value={$count} type="number"/>
stackblitz[bot] commented 1 year ago

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

netlify[bot] commented 1 year ago

Deploy Preview for sveltekit-search-params ready!

Name Link
Latest commit 0b0ff6d342771ccb3f11cd12deb3a299dd78515b
Latest deploy log https://app.netlify.com/sites/sveltekit-search-params/deploys/6380ef5354daea000966b019
Deploy Preview https://deploy-preview-4--sveltekit-search-params.netlify.app/
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.