location-query-params
v0.1.3
Published
Library to fetch query params from the Window's Location API
Downloads
24
Maintainers
Readme
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);