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-query-params

v1.0.9

Published

Easy to use query param support for React

Downloads

2,745

Readme

React Query Params

Easy to use query param support for React.

Plays super nice with React:

  • Semantically similar to setState()
  • Get/set arrays and objects
  • Clean URL's with default values
  • Opt in to browser history
  • Optionally read next query params from componentWillReceiveProps()

Installation

Install with npm:

npm install react-query-params

Setup

To be as seamless as possible ReactQueryParams is used as a base class were you would normally use Component. It inherits Component so you're not losing anything. Ex. App.jsx:

import ReactQueryParams from 'react-query-params';

export default class MyApp extends ReactQueryParams {
  /* Your code */
}

Usage

Get

const value = this.queryParams.lunch;

Set

Semantically similar to React setState():

this.setQueryParams({
	game: 'chess',
	algorithm: 'neural network'
});

When you set query param values an update is triggered just like setState().

setQueryParams() does not have or need an asynchronous variation.

Important

If a value is in a query param do not put it in state. The query param should own the value and be the source of truth for the value.

defaultQueryParams (optional)

Semantically similar to React defaultProps.

import ReactQueryParams from 'react-query-params';

export default class MyApp extends ReactQueryParams {
    defaultQueryParams = {
        game: 'go'
    }
}

When you define a default value the query param can be omitted from the URL but it's value is available just like any other query param.

Clean URL's with defaultQueryParams

By defining defaults for all your query params your app can load with a clean address having no explicit query string! If a query param is omitted from the URL query string it's default value is used. When you programmatically set a query param to its default value it will be removed from the URL query string. When you programmatically set a query param to a non-default value it will be added back to the URL.

Array and Object Values

Set

React-query-params does not use the repeating param approach for representing arrays on the URL, but instead stringifies arrays and objects for URL storage. Saves characters, more flexible, marvelous in every way.

const choices = ['chess', 'go'];
this.setQueryParams({ games: choices });
const first = this.queryParams.game[0];

Clear

To set an empty array use array literal syntax []. Don't set to null or empty string. React-query-params detects arrays and objects based on enclosing "[]" or "{}" characters.

this.setQueryParams({ games: [] });

Default value as Array or Object

ReactQueryParams has built in support for objects and arrays. However, there is one and only one case where you need to stringify the value yourself. When setting values in defaultQueryParams you must stringify objects and arrays yourself. JSON.stringify() can be a less error prone way to do it.

defaultQueryParams = {
	games: JSON.stringify(['chess','go'])
}

getQueryParam(name, source) (optional)

Method getQueryParam() reads one param value at a time but you can specify an alternate source. For instance you can read future query param values from React lifecycle hook componentWillReceiveProps().

componentWillReceiveProps(nextProps) {
	const myParamVal = this.getQueryParam('myparam', nextProps);
}

Support

Jeff Butsch

Software Engineer at Netflix

Email [email protected]

twitter @jeff3dx

License: MIT