useQueryParams.ts 592 B

123456789101112
  1. import { useCallback, useMemo } from 'react';
  2. import { useLocation } from 'react-router-dom';
  3. import { UrlQueryMap } from '@grafana/data';
  4. import { locationSearchToObject, locationService } from '@grafana/runtime';
  5. export function useQueryParams(): [UrlQueryMap, (values: UrlQueryMap, replace?: boolean) => void] {
  6. const { search } = useLocation();
  7. const queryParams = useMemo(() => locationSearchToObject(search || ''), [search]);
  8. const update = useCallback((values: UrlQueryMap, replace?: boolean) => locationService.partial(values, replace), []);
  9. return [queryParams, update];
  10. }