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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@veebisepad/react-query-filters

v1.0.0

Published

Utility hook for managing URL query parameters in React.

Readme

React Query Filters

@veebisepad/react-query-filters is a utility hook for managing URL query parameters in React.

It works well with Spatie laravel-query-builder and Inertia.js for filtering and sorting data in Laravel applications.

Installation

npm install @veebisepad/react-query-filters

Usage at a glance

React state is immutable, so the hook returns values (read in render) plus methods to change them:

  • read: filters.values.name
  • text/select input: {...filters.bind('name')}
  • toggle a value in a multiple filter: filters.toggle('cat', value)
  • apply: filters.get()

Features

  • URL-synced filters: initial state is parsed from the current URL query string.
  • Filter types via the factory:
    • single: a single value (text input, dropdown).
    • multiple: an array of values (multi-select, checkboxes).
    • range: a { from, to } range (date/price ranges).
    • custom: a fully custom filter.
  • Utility methods: get, toQueryObject, toOrderedQueryObject, toSearchParams, clear, clearAll, has, data.

API

useFilters(filters, options)

Initializes filters from the URL and returns their values plus methods.

Parameters

  • filters: object mapping each filter name to a filter created with the factory.
import { useFilters, factory } from '@veebisepad/react-query-filters';

const filters = useFilters({
    name: factory.single<string>(),
    category: factory.multiple<string>(),
    price: factory.range<number>(),
});
  • options (optional):
    • delimiter (default ','): separator for multiple/range values in the URL.
    • onApply(queryObject): called by get() with the current query object.
    • preserveQueryOrder (default true): keep query params in their URL order.
    • location: a Location/URL-like object to use instead of window.location (SSR).
const filters = useFilters(
    {
        name: factory.single<string>(),
        category: factory.multiple<string>(),
        price: factory.range<number>(),
    },
    {
        onApply(queryObject) {
            fetch('/api/products?' + new URLSearchParams(queryObject))
                .then(res => res.json())
                .then(console.log);
        },
    },
);

Returns

An object with the current values and the methods below.

Methods

set(key, value)

Sets a single filter value (triggers a re-render).

filters.set('name', 'John');

toggle(key, value)

Adds or removes a value from a multiple filter — ideal for checkboxes.

filters.toggle('category', 'electronics');

bind(key)

Returns { value, onChange } props for a controlled input bound to a single filter.

<input {...filters.bind('name')} />

get()

Triggers the onApply callback with the current filter values.

toQueryObject(transformKeys = true)

Converts current values into a plain query object.

filters.toQueryObject();
// { name: 'John', category: 'electronics,furniture', price: '100,500' }

toOrderedQueryObject(transformKeys = true)

Same as toQueryObject, but preserves the parameter order from the current URL.

toSearchParams()

Returns a URLSearchParams object built from the current values.

has(filter, value)

Checks whether a filter contains a specific value.

clear(filter, shouldGet = true)

Resets one filter (or an array of filters) to its default. Calls get() afterwards if an onApply callback is set and shouldGet is true.

clearAll()

Resets all filters to their defaults.

data()

Returns the current values object.

setOptions(newOptions)

Merges new options into the current ones.

Key transformation

Use createFilterFactory with a keyTransformer to map filter keys to URL parameter names — handy for Spatie's filter[...] convention:

import { useFilters, createFilterFactory } from '@veebisepad/react-query-filters';

const factory = createFilterFactory({
    keyTransformer: key => `filter[${key}]`,
});

const filters = useFilters({
    name: factory.single<string>(), // serialized as filter[name]
    category: factory.multiple<string>(), // serialized as filter[category]
});

Example Usage

React + Inertia.js + Spatie QueryBuilder

import { useFilters, createFilterFactory } from '@veebisepad/react-query-filters';
import { router } from '@inertiajs/react';

const factory = createFilterFactory({ keyTransformer: key => `filter[${key}]` });

export default function Products() {
    const filters = useFilters(
        {
            name: factory.single<string>(),
            category: factory.multiple<string>(),
            date: factory.range<string>(),
        },
        {
            onApply(queryObject) {
                router.visit(route('products.index', queryObject), {
                    preserveScroll: true,
                    preserveState: true,
                });
            },
        },
    );

    return (
        <div>
            {/* Single value filter (text input) */}
            <label>
                Name:
                <input {...filters.bind('name')} type="text" placeholder="Enter name" />
            </label>

            {/* Multiple values filter (checkboxes) */}
            <fieldset>
                <legend>Category</legend>
                {['electronics', 'furniture', 'apparel'].map(cat => (
                    <label key={cat}>
                        <input
                            type="checkbox"
                            checked={filters.has('category', cat)}
                            onChange={() => filters.toggle('category', cat)}
                        />
                        {cat}
                    </label>
                ))}
            </fieldset>

            {/* Range filter (date range) */}
            <label>
                From:
                <input
                    type="date"
                    value={filters.values.date.from ?? ''}
                    onChange={e => filters.set('date', { ...filters.values.date, from: e.target.value })}
                />
            </label>
            <label>
                To:
                <input
                    type="date"
                    value={filters.values.date.to ?? ''}
                    onChange={e => filters.set('date', { ...filters.values.date, to: e.target.value })}
                />
            </label>

            <button onClick={() => filters.get()}>Apply Filters</button>
            <button onClick={() => filters.clearAll()}>Clear All</button>
        </div>
    );
}

License

MIT