Course contentsShow
← React
Lesson 2263 of 2,39649. Performance: Identifying ProblemsFree lesson

React-Specific Metrics: useReportWebVitals

Implement custom hooks to track React-specific events like route changes, component mount times, and interaction delays.

React-Specific Metrics: useReportWebVitals

You'll learn: How to create custom hooks that capture React-specific performance events beyond standard Web Vitals, like route changes, component mount times, and interaction delays.

Beyond Generic Web Vitals

While the web-vitals library tracks browser-level metrics (LCP, FID, CLS), your React app has its own unique lifecycle events that matter for user experience. Route transitions, how long components take to mount, and the delay between user actions and UI responses are all React-specific performance signals you need to track.

The Custom Hook Pattern

You've already seen how to report standard Web Vitals. Now extend that approach with a custom hook that captures React events:

import { useEffect, useRef } from 'react';

export function useReportWebVitals(metric) {
  const startTimeRef = useRef(performance.now());

  useEffect(() => {
    // Track component mount duration
    const mountTime = performance.now() - startTimeRef.current;
    
    metric({
      name: 'component-mount',
      value: mountTime,
      id: 'unique-component-id'
    });
  }, []); // Empty array = mount only

  // Track interaction delays
  const trackInteraction = (eventName) => {
    const start = performance.now();
    return () => {
      const delay = performance.now() - start;
      metric({
        name: `interaction-delay-${eventName}`,
        value: delay,
        id: eventName
      });
    };
  };

  return { trackInteraction };
}

This hook measures how long a component takes from render to mount completion, then provides a utility to track specific user interactions. You can extend it to listen for route changes using your router's events, or track long-running effects.

Key Takeaway: Custom hooks like useReportWebVitals capture React-specific performance data that standard Web Vitals miss—component lifecycles, routing transitions, and interaction responsiveness—giving you a complete picture of your app's real-world performance.