Last active
March 5, 2021 13:38
-
-
Save DimitryDushkin/5ae91afbc5a51e4fb77772546551dfc5 to your computer and use it in GitHub Desktop.
React router utility functions to add and remove queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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