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

@kimaramyz/use-query-params

v0.3.3

Published

React hooks for using URL query params as state. No dependencies.

Downloads

30

Readme

@kimaramyz/use-query-params

@kimaramyz/use-query-params is a library of React hooks for using URL query params as state. Light-weight, TS support and no dependencies. This allows you to easily synchronize(encode and decode) react state with URL query parameters. Implemented by History API and URLSearchParams API.

Background

When creating apps with easily shareable URLs, you often want to encode state as query parameters, but all query parameters must be encoded as strings.

If you are doing a React-based project, you will probably be using ReactRouter or NextRouter together. However, the part where these routers use query string as a state, i.e., useSearchParams, returns instance of URLSearchParams, so you may need to parse it again. Therefore, this library can help you when you're trying to use query params with ReactRouter or NextRouter, and the other History API-based router libraries.

Features

  • Provides three options for using query params.

  • Provides useful options

    • Shallow routing - Able to update query parameter with no history changes
    • Hard/Soft key-value deletion (Upcoming)
  • No adapter required

  • Typescript support

  • Light-weight (5KB)

  • No dependencies.

    • No serializer library dependent
    • No router library dependent
    • Perfectly compatible with any version of ReactRouter

API

useQueryParams

declare function useQueryParams<KeyEnum extends string>(options?: {
  isShallow?: boolean;
}): [
  { [key in KeyEnum]?: string | undefined },
  (queryParams: { [key in KeyEnum]?: unknown }) => void
];

Basic example

import { FC } from 'react';
import { useQueryParams } from '@kimaramyz/use-query-params';

const BasicExample: FC = () => {
  const [queryParams, setQueryParams] = useQueryParams<'page' | 'q'>();

  return (
    <div>
      <h1>queryParams: {JSON.stringify(queryParams, null, 2)}</h1>
      <button onClick={() => setQueryParams({ ...queryParams, page: 2 })}>
        Upsert pageParam
      </button>
      <button
        onClick={() => setQueryParams({ ...queryParams, page: undefined })}
      >
        Delete pageParam
      </button>
      <button onClick={() => setQueryParams(null)}>Clear</button>
    </div>
  );
};

Using shallow routing

import { FC } from 'react';
import { useQueryParams } from '@kimaramyz/use-query-params';

const ShallowRoutingExample: FC = () => {
  const [queryParams, setQueryParams] = useQueryParams<'page' | 'q'>({
    isShallow: true,
  });

  return (
    <div>
      <h1>queryParams: {JSON.stringify(queryParams, null, 2)}</h1>
      <h2>history.length: {window.history.length}</h2>
      <button onClick={() => setQueryParams({ ...queryParams, page: 2 })}>
        Upsert pageParam
      </button>
      <button
        onClick={() => setQueryParams({ ...queryParams, page: undefined })}
      >
        Delete pageParam
      </button>
      <button onClick={() => setQueryParams(null)}>Clear</button>
    </div>
  );
};

useQueryParam

declare function useQueryParam<T = string>(
  key: string,
  options?: {
    isShallow?: boolean;
  }
): [T | null | undefined, (value: unknown) => void];

Basic example

import { FC } from 'react';
import { useQueryParam } from '@kimaramyz/use-query-params';

const BasicExample: FC = () => {
  const [page, setPage] = useQueryParam('page');

  return (
    <div>
      <h1>page: {page}</h1>
      <button onClick={() => setPage(1)}>Upsert</button>
      <button onClick={() => setPage(undefined)}>Clear</button>
    </div>
};

Using shallow routing

import { FC } from 'react';
import { useQueryParam } from '@kimaramyz/use-query-params';

const ShallowRoutingExample: FC = () => {
  const [page, setPage] = useQueryParam('page', { isShallow: true });

  return (
    <div>
      <h1>page: {page}</h1>
      <h2>history.length: {window.history.length}</h2>
      <button onClick={() => setPage(1)}>Upsert</button>
      <button onClick={() => setPage(undefined)}>Clear</button>
    </div>
  );
};

useQueryString

declare function useQueryString(options?: {
  isShallow?: boolean;
}): [
  string,
  (queryString: string | null | undefined, historyState?: unknown) => void
];

Basic example

import { FC } from 'react';
import { useQueryString } from '@kimaramyz/use-query-params';

const BasicExample: FC = () => {
  const [queryString, setQueryString] = useQueryString();

  return (
    <div>
      <h1>queryString: {queryString}</h1>
      <button onClick={() => setQueryString('?page=1&q=foo')}>Upsert</button>
      <button onClick={() => setQueryString(undefined)}>Clear</button>
    </div>
  );
};

Using shallow routing

import { FC } from 'react';
import { useQueryString } from '@kimaramyz/use-query-params';

const ShallowRoutingExample: FC = () => {
  const [queryString, setQueryString] = useQueryString({ isShallow: true });

  return (
    <div>
      <h1>queryString: {queryString}</h1>
      <h2>history.length: {window.history.length}</h2>
      <button onClick={() => setQueryString('?page=1&q=foo')}>Upsert</button>
      <button onClick={() => setQueryString(undefined)}>Clear</button>
    </div>
  );
};