Home / Fix guides / Long main-thread tasks blocking interaction / React

React fix guide

How to avoid long main-thread tasks on a React site

A long task is JavaScript that keeps the browser busy for over 50 milliseconds, so clicks and typing feel stuck. This guide shows how to find the cause in a React app and fix it safely.

When a page freezes for a moment after you tap a button, the cause is usually a long task: one chunk of JavaScript that holds the browser's main thread for more than 50 milliseconds. During that time the page cannot respond to clicks, taps or typing. Google measures this through Interaction to Next Paint (INP), and Lighthouse shows a related number called Total Blocking Time. React apps are prone to it because a lot of work (rendering, hydration, your own code) happens in JavaScript.

Where long tasks usually come from in React

  • A large JavaScript bundle that has to be downloaded, parsed and run before the page is usable.
  • A state change that re-renders a big part of the component tree.
  • Heavy calculations, filtering or sorting done directly inside a component.
  • Long lists or tables rendered all at once.
  • Hydration of a server rendered page (for example in Next.js) running as one big block.
  • Third party scripts such as chat widgets, analytics and tag managers.

Step by step

  1. Test the production build. React's development mode is much slower than production and adds extra checks. Run your build command (usually npm run build) and test the result, or test the live site. Never judge speed from npm start or a dev server.
  1. Record a trace. Open the page in Chrome, open DevTools, go to the Performance panel, press record, do the slow action (click the menu, type in the search box), then stop. Long tasks are marked with a red corner in the main thread track. Click one and look at the Bottom-Up tab to see which functions took the time.
  1. Find out whose code it is. If the time is in your own components, the React DevTools Profiler (a browser extension) shows which components rendered and how long each took. If it is in files from another domain, it is a third party script. If it happens right at page load, it is bundle size or hydration.
  1. Load less JavaScript up front. Split code that is not needed on first view, such as modals, charts, editors and rarely visited routes, with React.lazy and Suspense:
import { lazy, Suspense } from 'react';
const Chart = lazy(() => import('./Chart'));

<Suspense fallback={<p>Loading...</p>}>
  <Chart />
</Suspense>

Route level splitting is built in to frameworks like Next.js, but components you import by hand still need this. Our guide to unused JavaScript on React covers how to find code that loads but is never used.

  1. Mark slow updates as non-urgent. In React 18 and later, wrap updates that can wait in startTransition (or use the useTransition hook) so typing and clicks stay responsive:
const [isPending, startTransition] = useTransition();

function onChange(e) {
  setQuery(e.target.value);              // urgent: the input itself
  startTransition(() => setFilter(e.target.value)); // can wait
}

useDeferredValue does a similar job when you receive a value as a prop. This does not make the work cheaper, it lets React interrupt it so the browser can handle input first.

  1. Make renders cheaper. Move state down to the smallest component that needs it, so fewer components re-render. Use React.memo, useMemo and useCallback where the profiler shows repeated expensive renders, not everywhere by default. Give list items stable key values. For lists with hundreds or thousands of rows, render only what is visible with a windowing library such as react-window.
  1. Break up heavy work. If you must run a long loop, split it into chunks and give the browser a chance to breathe between them. scheduler.yield() does this in newer Chromium browsers, with setTimeout(resolve, 0) as a fallback elsewhere. For truly heavy calculations (parsing large files, image processing), move them into a Web Worker, which runs off the main thread.
  1. Deal with third party scripts. Load them after the page is interactive, remove ones you no longer use, and check whether a lighter option exists. In Next.js the next/script component with the lazyOnload strategy helps. More on this in third party scripts on React.
  1. Measure again. Repeat the same recording on the production build. Fewer red long task markers and a lower Total Blocking Time mean it worked. Fix one thing at a time so you know what helped.

What React does not do for you

React does not split your code automatically, does not stop you from rendering a huge list, and does not know which updates matter most unless you tell it with transitions. Server rendering improves how quickly content appears, but the page still has to hydrate, which is JavaScript work on the main thread.

When to call a developer

Steps 1 to 3 and 8 are doable for most site owners. Steps 4 to 7 involve changing component code, so hand your DevTools recording to your developer and ask them to start with the longest tasks.

SiteMonday checks speed and Core Web Vitals in week 4 and lists what to fix in a ranked report. For the general explanation of this problem on any platform, see long JavaScript tasks.

Not on React? Read the general guide to long main-thread tasks blocking interaction, or the guide for Next.js, WordPress, Shopify, Wix, Google Tag Manager, Vercel.

Questions

Is a long task the same as a slow React render?

Not always. A slow render is one possible cause, but long tasks can also come from parsing big bundles, hydration, your own calculations or third party scripts. The Performance panel in Chrome DevTools shows which one it is.

Do I need React 18 to use startTransition?

Yes, `startTransition` and `useTransition` arrived in React 18. On older versions, upgrade first, or split the work into chunks and use a Web Worker.

Will wrapping everything in React.memo fix it?

No. Memoization only helps when the same props lead to repeated expensive renders, and it adds its own overhead. Use the React DevTools Profiler to find the components that actually need it.

Why is my app fast in testing but slow for real visitors?

Real visitors often use slower phones and have less CPU power than a developer laptop. In Chrome DevTools you can set CPU throttling (for example 4x slowdown) in the Performance panel to get closer to their experience.

Does server side rendering remove long tasks?

No, it mainly helps content show up sooner. The browser still has to download and run React to hydrate the page, and that can be a long task if the bundle is large.

More React fixes