diff options
Diffstat (limited to 'src/hooks/useQuery.ts')
-rw-r--r-- | src/hooks/useQuery.ts | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/hooks/useQuery.ts b/src/hooks/useQuery.ts index f025a46..e9a00ec 100644 --- a/src/hooks/useQuery.ts +++ b/src/hooks/useQuery.ts @@ -1,9 +1,21 @@ -import { useLocation } from 'react-router-dom'; +import { useHistory } from 'react-router-dom'; -const useQuery = (): Record<string, string> => { - const location = useLocation(); - const searchParams = new URLSearchParams(location.search); - return Object.fromEntries(searchParams); +interface UseQuery { + query: Record<string, string>; + setQuery: (query: Record<string, string>) => void; +} + +const useQuery = (): UseQuery => { + const history = useHistory(); + const searchParams = new URLSearchParams(history.location.search); + const query = Object.fromEntries(searchParams); + + const setQuery = (newQuery: Record<string, string>): void => { + const queryString = new URLSearchParams(newQuery); + history.push({ search: `?${queryString}` }); + }; + + return { query, setQuery }; }; export default useQuery; |