location-query-params
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

Location Query Params

Library to fetch query params from the Window's Location API & React-Router

Install

npm install location-query-params

Examples

Get all Query param values

const { getQueryParams } = require("location-query-params");
const queryParams = getQueryParams(location);

This will return the following object

// With a url like - http://localhost:3000/staff/projects/1/jobs?apples=1&bananas=3&oranges=true&plums=hello
{
    apples: { name: 'apples', value: 1 },
    bananas: { name: 'bananas', value: 3 },
    oranges: { name: 'oranges', value: true },
    plums: { name: 'plums', value: 'hello' }
}

Get a single query param value

If you only care about getting single value back from a url containing query param(s) you can use the getQueryParamByName function. For example:

let result = getQueryParamByName("apples", location);
// result is 1

React Router

If you have React Router setup in your app then you can access the location API from the react-router wrapped component props. For example:

const { location } = props;
let queryParams = getQueryParams(location);

Package Sidebar

Install

npm i location-query-params

Weekly Downloads

2

Version

0.1.3

License

MIT

Unpacked Size

8.7 kB

Total Files

12

Last publish

Collaborators

  • joegasewicz