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

react-filter-by-url

v0.3.0

Published

A React library to update API Url with query parameters by looking at query parameters in the address bar of browser.

Downloads

158

Readme

npm npm lisence npm type definitions

react-filter-by-url

Try it now:

Edit bold-ellis-6rg1t

Live demo: https://react-filter-by-ulr-demo.vercel.app/list

Problem

This is the URL with query parameters in your browser address bar:

https://example.com/search?page=2&type=public&status=open

And you want to call an API with the exact same query parameters:

https://example.com/api/search?page=2&type=public&status=open

API url with query parameters is updated automatically by:

  • Paste in the link of the URL with query parameters to the address bar in browser
  • Change the URL query parameters directly in the browser
  • Update Filter Options in the UI

useFilter

Installation

yarn add react-filter-by-url

Implementation

import { useUrlFilter } from 'react-filter-by-url'
interface ListProps {}

const DemoList: React.FC<ListProps> = ({}) => {
	// api url
	const apiUrl = 'https://rickandmortyapi.com/api/character'

	// list of params to filter
	const params = ['page', 'status']

	const { apiQuery, getDefaultParamValue, handleSelectFilter } = useUrlFilter(
		params,
		apiUrl
	)

	return <>
		...
	</>
}

You will need to pass in this hook:

| Option | Description | | --------------- | --------------------------------------------------------------- | | params | An array of string, define all the query parameters the API has. | | apiUrl | A string, the base url of the API without the query parameters. | | refreshParams | (Optional) an Array of string, represents query params need to refresh to the defaul ones. Ex: refresh page=1 when there is a change in other query params |

You will have access to the following values:

| Option | Description | | --------------- | --------------------------------------------------------------- | | apiQuery | A string, API url with the query parameters (same query parameters with browser). | | getDefaultParamValue | A function, get the default value of a query param. | | handleSelectFilter| A function, handle the change in the filter options in the UI. | | queryString| A string, the recent query parameters. |

Try toggling around the filters in the UI by using handleSelectFilter to update the URL query parameters:

<select
	name='status'
	onChange={e =>
		handleSelectFilter(e.target.name, e.target.value)
	}
	defaultValue={getDefaultParamValue('status', '')}
>
	<option value=''>All</option>
	<option value='Alive'>Alive</option>
	<option value='Dead'>Dead</option>
	<option value='unknown'>unknown</option>
</select>

Then you can simply call API every time the apiQuery changes which means URL query parameters change.

useEffect(() => {
	fetchApi()
}, [apiQuery])

const fetchApi = async () => {
	const response = await fetch(apiQuery)
	const data = await response.json()
}

Now you can try to change the URL query parameters in the browser or tinker around the filter UI and see the result.

Live demo: https://react-filter-by-ulr-demo.vercel.app/list

License

MIT License