User Interaction Performance Events

Instrument user interactions such as clicks or form submissions in Next.js to measure and improve UI performance.
import { useEffect } from 'react';

// Utility function to log performance events
const logPerformance = (eventName, duration) => {
  console.log(`Performance event: ${eventName}, Duration: ${duration}ms`);
  // Here you could send the performance data to an analytics endpoint

// Hook to monitor clicks and log their performance
const useClickPerformance = () => {
  useEffect(() => {
    const handleClick = (event) => {
      const start =;
      // Potentially do some work here
      const duration = - start;
      logPerformance('click', duration);
    document.addEventListener('click', handleClick);
    return () => document.removeEventListener('click', handleClick);
  }, []);

export default useClickPerformance;
This piece of code defines a custom React hook `useClickPerformance` that uses the browser's Performance API to measure the time taken to process a click event, then logs that duration using a utility function `logPerformance`. The hook adds a click event listener when a component mounts and removes it on unmount.
import { useForm } from 'react-hook-form';

// Component using the custom hook to track click performance
const FormComponent = () => {
  const { register, handleSubmit } = useForm();
  useClickPerformance(); // activates the click performance tracking

  const onSubmit = (data) => {
    const start =;
    // Submit form data here
    // ... (e.g., send data to an API)
    const duration = - start;
    logPerformance('form submission', duration);

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register('example')} />
      <input type='submit' />

export default FormComponent;
Here we have a `FormComponent` that utilizes the `useClickPerformance` hook to track clicks. It also uses the `react-hook-form` library for form handling. The `onSubmit` function measures the time taken for a form submission process, employing the Performance API and logs the performance with the `logPerformance` function.