Created
November 29, 2020 12:25
-
-
Save mortezasabihi/9781ae0c5d534b44280f2542ebc75804 to your computer and use it in GitHub Desktop.
React query string hook using URLSearchParams
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 { 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