← Back to React, page 5

react-0082 · Medium mid

What is a request waterfall in React, why is it a problem, and how do you fetch data in parallel and avoid fetch-on-render?

A waterfall happens when requests run one after another because each starts only after the previous finished or after a component mounted. Total time is the sum of all waits. Fix it by starting independent requests together (Promise.all or hoisting), fetching before render (route loaders, Server Components, prefetching) and using Suspense with use() so rendering does not decide when fetching starts. Fetch-on-render in nested components is the usual cause.

Full answer

Fetch-on-render is the common beginner pattern: a component mounts, an effect starts a fetch, and the data arrives later. If a child component also fetches, it only mounts after its parent has rendered real data, so its fetch starts after the parent's finished. With three levels of nesting, three round trips happen in sequence. This is a waterfall, and it makes pages slow on high latency networks.

Ways to avoid it:

  1. Parallelize independent requests. Start them together and wait for both: const [user, courses] = await Promise.all([getUser(), getCourses()]). Use Promise.allSettled when one failure should not hide the other result. Only true dependencies should be sequential.
  2. Fetch earlier. Route loaders (React Router, Next.js Server Components, TanStack Router) start fetching when navigation begins, before the component renders. This is called render-as-you-fetch.
  3. Hoist and pass promises. Start the promise in a parent or loader and pass it to children, which read it with use(promise) inside a Suspense boundary. Create the promise outside the component or cache it, or each render creates a new request.
  4. Prefetch. TanStack Query's prefetchQuery, link hover prefetching and <link rel="preload"> start requests before the user needs the data.
  5. Move the join to the server. One endpoint (or GraphQL query) returning what the page needs replaces several round trips.

Related correctness issues: handle races (see the abort pattern), show skeleton fallbacks that match the layout, and avoid fetching on the client what could be rendered on the server.

Measure with the browser network panel: a staircase of bars is a waterfall.

The example measures sequential and parallel loading with two 50 ms requests.

jsx
const delay = (ms, value) => new Promise((resolve) => setTimeout(() => resolve(value), ms));
const getUser = () => delay(50, { name: 'Asha' });
const getCourses = () => delay(50, ['Maths', 'Science']);

async function timed(label, load) {
  const start = performance.now();
  const [user, courses] = await load();
  const elapsed = performance.now() - start;
  console.log(label + ': ' + user.name + ' has ' + courses.length + ' courses, took at least 90 ms: ' + (elapsed >= 90));
}

await timed('sequential', async () => {
  const user = await getUser();
  const courses = await getCourses();
  return [user, courses];
});

await timed('parallel', () => Promise.all([getUser(), getCourses()]));

This ran as a script with two simulated requests of 50 ms. The sequential version needs about 100 ms and the parallel one about 50 ms. Output of the run: sequential: Asha has 2 courses, took at least 90 ms: true parallel: Asha has 2 courses, took at least 90 ms: false

Key points

  • Waterfalls make total time the sum of requests, usually from fetch-on-render in nested components.
  • Start independent requests together with Promise.all, and keep sequence only for real dependencies.
  • Fetch before render with loaders, Server Components and prefetching.
  • Pass promises to Suspense with use(), created outside render.
  • Reduce round trips with server-side joins or a combined endpoint.

Common mistakes

  • Nesting components that each fetch in an effect after the parent has data.
  • Awaiting independent requests one after another.
  • Creating the promise inside a component that uses use(), restarting it every render.
  • Using Promise.all when a single failure should not block the page.
  • Measuring only on a fast local network and missing the waterfall.

Follow-ups

  • How does Suspense with use() change fetch-on-render?
  • How do route loaders in React Router or Next.js help?
  • When is a waterfall unavoidable?

Tips

  • Define the term with the nested component example.
  • List several fixes, from simple to structural.
  • Mention measuring with the network panel.