-
Notifications
You must be signed in to change notification settings - Fork 1
Guide: Adding URL parameter support
Many of our tasks involve supporting URL query parameters by setting and using them. This helps create unique, shareable URLs for the same page, particularly ones with sorting and filtering options.
For the best coding and debugging experience, you should familiarize yourself with these libraries and functions:
- React Router, especially:
-
query-string, especially:
- the
.parse()and.stringify()functions - parse options like
parseNumbersandparseBooleans - support for multiple instances of the same key (see second example here)
- the
- JavaScript, especially:
Most setting/clearing/pruning of query params will look the same. Consider reusing these functions in the pages you need them:
Generically sets the given URL parameter key to the given val. Creates a history entry.
const setParam = (key, val) => {
history.push({
search: queryString.stringify({...params, [key]: val})
});
}Generically clears the given URL parameter key to the given val. Creates a history entry.
const clearParam = (key) => {
delete params[key];
history.push({search: queryString.stringify(params)});
}Generically clears the given URL parameter key to the given val. Replaces the current history entry.
const pruneParam = (key) => {
delete params[key];
history.replace({search: queryString.stringify(params)});
}- Parse the URL parameters. You may want to specify options.
const history = useHistory();
const location = useLocation();
const params = queryString.parse(location.search);-
Validate the parameter key/value you're working with, e.g. by checking its type and/or whether it's in a set of values.
-
If the parameter value is invalid, prune the parameter key from the URL query string by calling
pruneParam(key)or by defining your own custom pruning function. Note that this step should replace the current history entry (so that users don't click back to a page with bad parameters). -
If the
setParam(key, val)andclearParam(key)functions aren't enough for wherever the URL parameter key/value needs to be updated, define your own custom updater function that can be used as a callback. E.g.
// Parse params...
const selectedIndex = params.selectedIndex;
// Validate selectedIndex...
const onSelectItem = (index) => {
if (index === selectedIndex) {
clearParam('selectedIndex');
} else {
setParam('selectedIndex', index);
}
}- Use the parameter key/value and its updater function wherever needed. You will likely need to define wrapper functions around them if it doesn't make sense for a child component to understand query parameters. E.g.
const setFoo = (e) => setParam('foo', e.target.value);
return (
{/* ... */}
<Dropdown onChange={setFoo}>
{/* ... */}
)