url-parameters
Easily manage and listen for changes in query parameters, with full support for the browser history API.
Installation
npm install url-parameters --save
yarn add url-parameters
Usage
import url from 'url-parameters';
url.enable(onChange => {
console.log(onChange.queryParams);
console.log(onChange.queryString);
});
Disabling Push State
You can disable the Push State by setting the second parameter to false
when enabling the url listener:
url.enable(onChangeCallback, false);
This will add a #
before the parameters in the URL and will prevent a page reload when the parameters are changed.
Modifying parameters and their values
All of these methods will trigger the listener callback in the enable()
method demonstrated above which provides the updated parameters as an object and as the full query string.
url.set('param', 'value')
url.toggle('param', 'value')
url.toggleValue('param', 'value')
url.apply('param=value¶m2=value2')
url.apply({param: 'value', param2: 'value2'})
url.replace({param1: value1, param2: value2})
url.remove('param')
url.clear()
Accessing parameters and their values directly
url.get('param')
url.get('param', 'default')
url.containsValue('param', 'value')
url.getQueryString()
url.getParams()