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

@dragonraider5/react-router-query-params

v1.1.0

Published

Set query parameters with a schema for react-router.

Downloads

8

Readme

@dragonraider5/react-router-query-params

npm version Download Count

Set query parameters with a schema for react-router.

See react-router-query-params for the original package.

This fork uses Reacts forwardRef (available from [email protected] upwards) to allow for sane ref usage with the HOC exposed by this package.

In addition it fixes the usage of the new react-router history API, which doesn't require history.createHref() anymore and breaks hash routers when being used the old way.

Install

npm i @dragonraider5/react-router-query-params

Peer dependencies

  • react v. ^16.3.0
  • react-router v. ^4.0.0 || ^5.0.0
  • react-router-dom v. ^4.0.0 || ^5.0.0

Example

import withQueryParams from 'react-router-query-params';
...

const ExampleComponent = ({
  queryParams,
  setQueryParams,
}) = (
  <div>
    <div>
      queryParams: {JSON.stringify(queryParams)}
    </div>

    <button onClick={() => setQueryParams({ example1: 'someQueryParam' })}>
      Set query param example
    </button>
  </div>
);

const ConnectedComponent = withQueryParams({
  stripUnknownKeys: false,
  keys: {
    example1: {
      default: 'example-1-default',
      validate: value => !!value && value.length > 3,
    },
    example2: {
      default: (value, props) => props.defaultValue,
      validate: (value, props) =>
        !!value && !props.disallowedValues.includes(value)
    }
  }
})(ExampleComponent);

API

Props

  • queryParams (object): All current query parameters as key-value pairs in an object.

  • setQueryParams (function): Set one or more query parameters.

this.props.setQueryParam({ key1: 'value1', key2: 'value2' })

HoC

The library exports withQueryParams higher order component as default. The HoC takes a configuration object as the first argument, and has the following options:

  • stripUnknownKeys (boolean)

    • if true, removes keys from query parameters that are not configured with keys
    • default: false
  • keys (object)

    • example:
      keys: {
        example: {
          default: 'default-value',
          validate: () => true
        }
      }

Key configuration object

Key object is used to create a configuration for the query parameters that are intended to be used. Every key is configured with the following properties:

  • default (any): Define the default value for the query parameter. If query parameter valiation fails or it is undefined, the HoC automatically sets the query parameter to this value. Examples:

    • default: 'example': sets 'example' as default value
    • default: (value, props) => props.defaultParam': sets defaultParam from the component props as default value
    • default: undefined: do not set query parameter at all by default
  • validate (function): Validate the query parameter and revert to default value if validation does not pass. Examples:

    • validate: () => true: allow any alue
    • validate: value => !!value && value.length > 2: allow any value with more than two characters
    • validate: (value, props) => props.allowedValues.includes(values): validate value based on props

License

MIT