Skip to content

Instantly share code, notes, and snippets.

@mortezasabihi
Created November 29, 2020 12:25
Show Gist options
  • Save mortezasabihi/9781ae0c5d534b44280f2542ebc75804 to your computer and use it in GitHub Desktop.
Save mortezasabihi/9781ae0c5d534b44280f2542ebc75804 to your computer and use it in GitHub Desktop.
React query string hook using URLSearchParams
import { useLocation } from "react-router-dom";
import PropTypes from "prop-types";
export default function useQueryParams(...keys) {
const location = useLocation();
const query = new URLSearchParams(location.search);
let paramObj = {};
for (let value of keys) {
paramObj[value] = query.get(value);
}
return paramObj;
}
useQueryParams.propTypes = {
key: PropTypes.string,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment