useURLSearchParams.ts 627 B

123456789101112131415161718
  1. import { useCallback, useMemo } from 'react';
  2. import { useLocation } from 'react-router-dom';
  3. import { locationService } from '@grafana/runtime';
  4. export function useURLSearchParams(): [
  5. URLSearchParams,
  6. (searchValues: Record<string, string | string[] | undefined>, replace?: boolean) => void
  7. ] {
  8. const { search } = useLocation();
  9. const queryParams = useMemo(() => new URLSearchParams(search), [search]);
  10. const update = useCallback((searchValues: Record<string, string | string[] | undefined>, replace?: boolean) => {
  11. locationService.partial(searchValues, replace);
  12. }, []);
  13. return [queryParams, update];
  14. }