npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-use-query-param

v1.0.2

Published

Use url query parameter like regular refs in Vue, in a typesafe way.

Downloads

202

Readme

IMPORTANT: This plugin has a peer dependency on vue-router.
It will only work when using vue-router in your Vue app.

Features

  • Get & set query parameters from the URL in a typesafe way
  • Use query parameters like regular ref's

Don't want to read the rest of the README? Check out the examples.

Installing

npm

npm install vue-use-query-param

pnpm

pnpm add vue-use-query-param

yarn

yarn add vue-use-query-param

Usage

Add the plugin to your Vue app

import { createApp } from "vue";
import { createRouter } from "vue-router";
import useQueryParamPlugin from "vue-use-query-param";

const router = createRouter({ ... });

const app = createApp(App);
app.use(router);
app.use(useQueryParamPlugin, {});
app.mount("#app");

Use the useQueryParam composable

<script setup lang="ts">
import { stringParam, useQueryParam } from "vue-use-query-param";

// foo is a `Ref<string | undefined>`, use it like any other ref
// When setting foo, the URL will be modified, a query parameter `foo` will
// be added or updated. (or removed if you're setting `foo` to undefined.)
const foo = useQueryParam("foo", stringParam());

foo.value = "bar";
</script>

<template>
  <!-- Reactively updates -->
  {{ foo }}
</template>

Non-nullable query parameters (default values)

If your query parameter should always have a value, you can set a default value like this:

import { stringParam, useQueryParam, withDefault } from "vue-use-query-param";
const foo = useQueryParam("foo", withDefault(stringParam(), "bar"));

Supported types

Currently only a few basic types are supported.

  • stringParam: for strings
  • numberParam: for numbers (integers and floats)
  • objectParam: custom Objects that will be serialized to JSON.
  • booleanParam: for booleans
  • dateParam: for dates and datetime
  • arrayParam: for arrays of the above types
    NOTE: Nested arrays are currently only possible using objectParam. e.g.: objectParam<number[][]>();

For some examples on how to use these, check out these examples.

Extra options

Debounce time

Internally, to be able to set multiple query parameters at once, we don't update the URL immediately after every change. We use setTimeout to debounce the updates.
By default the debounce time is set to 0, meaning it will update the URL as soon as possible.
If however you want to delay this more, you can set the debounceTime option when adding the plugin to the Vue app.

app.use(useQueryParamPlugin, { debounceTime: 100 });
...
const foo = useQueryParam("foo", stringParam());
const bar = useQueryParam("bar", stringParam());

// the `foo` and `bar` query parameters in the URL won't be updated immediately, but only after 150ms.
// (50 ms for the bar timeout + 100ms for the debounce time)
foo.value = "fooval";
setTimeout(() => {
  bar.value = "barval";
}, 50);

Credits

How to publish a TypeScript package to npm

TypeScript NPM Package Publishing: A Beginner’s Guide (by Paul Ehikhuemen)

Inspiration for this package

I'm originally a React dev and I made much use of the use-query-params hook.

Sponsors

ai-Gust

The original need for this plugin came when working on a project for ai-Gust. An initial implementation of useQueryParam was made there.
They were so kind to let me make this into a separate npm module and make it open source for the world to enjoy.
I've since built on this initial implementation.