Skip to content

Instantly share code, notes, and snippets.

@DimitryDushkin
Last active March 5, 2021 13:38
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DimitryDushkin/5ae91afbc5a51e4fb77772546551dfc5 to your computer and use it in GitHub Desktop.
Save DimitryDushkin/5ae91afbc5a51e4fb77772546551dfc5 to your computer and use it in GitHub Desktop.
React router utility functions to add and remove queries
import { browserHistory } from 'react-router';
/**
* @param {Object} query
*/
export const addQuery = (query) => {
const location = Object.assign({}, browserHistory.getCurrentLocation());
Object.assign(location.query, query);
browserHistory.push(location);
};
/**
* @param {Object} query
*/
export const replaceQuery = (query) => {
const location = Object.assign({}, browserHistory.getCurrentLocation());
Object.assign(location.query, query);
browserHistory.replace(location);
}
/**
* @param {...String} queryNames
*/
export const removeQuery = (...queryNames) => {
const location = Object.assign({}, browserHistory.getCurrentLocation());
queryNames.forEach(q => delete location.query[q]);
browserHistory.push(location);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment