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

query-history

v2.0.0

Published

A history with improved query parameter support.

Downloads

38

Readme

query-history

Travis build status npm version Test Coverage gzip size

A history with additional APIs to more easily manage query parameters using query-string.

Installation

Install using npm:

npm install query-history

or yarn:

yarn add query-history

Table of Contents

Guides

The following guides assume that you're using query-history with React Router.

Getting Started

Create a history object using the default export from this library, createQueryHistory.

import createQueryHistory from 'query-history';

const history = createQueryHistory();

createQueryHistory accepts all of the same option as history, so you can, for example, specify a basename option:

const history = createQueryHistory({
  basename: '/my-app',
});

For more information on the available options, refer to the history docs.

Once you have a history, you can then pass it into a Router from React Router.

import { Router } from 'react-router-dom';

// Later, in a component...
return (
  <Router history={history}>
    <App />
  </Router>
);

Reading query parameters

When using this library, the location object includes a new key, query. This is an object that represents the query parameters.

import { useLocation } from 'react-router-dom';

export default function MyComponent() {
  const { query } = useLocation();

  console.log('What is the current query?', query);

  return null;
}

For instance, given the URL https://app.com/?hello=true&sandwiches=tasty&size=40, the value of history.query would be:

{
  hello: 'true',
  sandwiches: 'tasty',
  size: '40'
}

Keep in mind that although this library will parse your query string as an object, by default the individual query string values are always strings. Notice how the size parameter is parsed as the string "40" in the example above.

You are either responsible for converting the values to their correct type in your application, or you can configure query-string to do it for you.

Updating query parameters

The history methods push and replace have been updated with improved support for updating query parameters. Additionally, a new method, updateQuery, has been introduced.

Use updateQuery to change the query parameters without redirecting to a new path.

import { useHistory } from 'react-router-dom';

export default function MyComponent() {
  const history = useHistory();

  function navigate() {
    history.updateQuery({
      sandwiches: 'tasty',
    });
  }

  return null;
}

This will merge the new params into the old. To replace the parameters, pass a second argument, { mergeQuery: false }:

history.updateQuery(
  {
    sandwiches: 'tasty',
  },
  {
    mergeQuery: false,
  }
);

When calling push or replace, pass an optional query object to specify new query parameters.

import { useHistory } from 'react-router-dom';

export default function MyComponent() {
  const history = useHistory();

  function navigate() {
    history.push({
      pathname: '/',
      query: {
        sandwiches: 'tasty',
      },
    });
  }

  return null;
}

The new parameters that you pass are merged with the existing ones. To replace the parameters, pass mergeQuery: false to history.push or history.replace:

history.push({
  pathname: '/',
  query: {
    sandwiches: 'tasty',
  },
  mergeQuery: false,
});

Removing Query Parameters

You can remove query parameters by passing their value as undefined. For example:

// Removes "sandwiches" from the query parameter list
history.updateQuery({
  sandwiches: undefined',
});

To learn more about how falsey values are parsed, refer to the query-string docs.

Configuring query parameter behavior

This library uses the query-string library to parse and serialize query parameters. To configure this behavior, the createQueryHistory function accepts two options:

  • parseOptions: an object of options passed into queryString.parse()
  • stringifyOptions: an object of options passed into queryString.stringify()

Click the options name in the above list to view all of the supported options.

Example: automatically parsing numbers and booleans

By default, numbers and booleans in the query parameter are parsed as strings. You can configure this library to parse them as their correct types using the following code:

import createQueryHistory from 'query-history';

const history = createQueryHistory({
  parseOptions: {
    parseNumbers: true,
    parseBooleans: true,
  },
});

Example: configuring how arrays are serialized and parsed

Different applications have different requirements when it comes to serializing arrays, and query-string supports a number of options. In this example, we set the array format to "comma". View all of the options in the query-string docs.

import createQueryHistory from 'query-history';

const history = createQueryHistory({
  stringifyOptions: {
    arrayFormat: 'comma',
  },
  parseOptions: {
    arrayFormat: 'comma
  }
});

Acknowledgements

This library was inspired by qhistory.