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

@use-form/use-query-filter

v1.2.0

Published

Push the filter state into the Url Query Params.

Downloads

15

Readme

useQueryFilter

Motivation

Recently I worked with some react applications, and our team needed to persist the filter's state into URL, and we needed a react hook to turn this work more easily, but we could not find anyone to do this.

When the filter's state is persisted in the URL the user receives a better experience. It's possible to use browser buttons to navigate in applications, also it's possible to refresh the browser tab and the result is the same because the filter's state is persisted in the URL, another good point is that, whit this approach it's possible to persist pagination, even because the pagination's state and filter's state should be the same.

How it's works

useQueryFilter uses react hook to persist the filter state in a component state and the same state is pushed into the URL. Then every state change is pushed into the URL and if the URL has some value or state when the component is mounting this Url state is propagated into the component state.

Instalation

npm i @use-form/use-query-filter or yarn add @use-form/use-query-filter

Demo

https://codesandbox.io/s/use-query-filter-iiend

Let's see some examples of use:

useQueryFilter

   const initialValues = {
      keywords:'',
      sort:'az'
   }

   const [filter, set, reset] = useQueryFilter(initialValues)

   <input name="keywords" onChange={e=>set({keywords:e.target.value})}/>

   <select onChange={e=>set({sort:e.target.value})}>
      <option value="az">A - Z</option>
      <option value="za">Z - A</option>
   </select>

useFilterContext

useFilterContext is helpful when is necessary to share the filter state with another component, in the same example is very common to have a filter component, a table component, and a pagination component, in this case, should have a container component and into this component, useFilterContext should be used like this example:

   const initialValues = {
      keywords:'',
      sort:'az'
   }

   function Container(){

      const filter = useQueryFilter(initialValues)

      return (
         <FilterContext.Provider value={filter}>
            <Filter/>
            <List/>
            <Pagination/>
         </FilterContext.Provider>
      )
   }

   function Filter(){

      const [filter,{set}] = useFilterContext()

      return(
         <>
            <input name="keywords" onChange={e=>set({keywords:e.target.value})}/>

            <select onChange={e=>set({sort:e.target.value})}>
               <option value="az">A - Z</option>
               <option value="za">Z - A</option>
            </select>
         </>
      )
   }
   function List(){

      const [filter,{set}] = useFilterContext()

      return(<>...</>)
   }
   function Pagination(){

      const [filter,{set}] = useFilterContext()

      return(<>...</>)
   }