0.1.3 • Published 3 years ago

location-query-params v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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);