useRouteAnalyticsParams.tsx 758 B

12345678910111213141516171819
  1. import {useContext, useEffect} from 'react';
  2. import {RouteAnalyticsContext} from 'sentry/views/routeAnalyticsContextProvider';
  3. /**
  4. * Sets the analyitcs params for route analytics events.
  5. * Must be called within 2s after the organization context is loaded.
  6. */
  7. export default function useRouteAnalyticsParams(params: Record<string, any>) {
  8. const {setRouteAnalyticsParams} = useContext(RouteAnalyticsContext);
  9. const keys = Object.keys(params);
  10. const values = Object.values(params);
  11. useEffect(() => {
  12. setRouteAnalyticsParams(params);
  13. // use the object values and keys as dependencies to re-trigger rendering
  14. // if the underlying parameters change
  15. // eslint-disable-next-line react-hooks/exhaustive-deps
  16. }, [...values, ...keys]);
  17. }