Performance monitoring tracks and optimizes app performance.
Web Vitals:
1import { useEffect } from "react";2import { onCLS, onFID, onLCP } from "web-vitals";34function reportWebVitals(metric) {5 console.log(metric);6 // Send to analytics7 analytics.track("web-vital", {8 name: metric.name,9 value: metric.value,10 id: metric.id,11 });12}1314// In _app.tsx or layout.tsx15export function reportWebVitals(metric) {16 switch (metric.name) {17 case "CLS": reportCLS(metric); break;18 case "FID": reportFID(metric); break;19 case "LCP": reportLCP(metric); break;20 }21}
Custom performance hook:
1function usePerformanceMonitor(name: string) {2 useEffect(() => {3 const start = performance.now();45 return () => {6 const duration = performance.now() - start;7 if (duration > 100) {8 console.warn(`Slow render: ${name} took ${duration}ms`);9 }10 };11 }, [name]);12}1314// Usage15function ExpensiveComponent() {16 usePerformanceMonitor("ExpensiveComponent");17 // ... render18}
React DevTools Profiler:
Best practices: