summaryrefslogtreecommitdiff
path: root/src/hooks/useQuery.ts
blob: e9a00eccb3072ed2c08394f36db97f62d5bee55c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { useHistory } from 'react-router-dom';

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;