Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
React Interview Essentials
  1. 1.Introduction to React & Basic Setup
  2. 2.JSX, Functional Components & Props
  3. 3.State Management with useState & useEffect
  4. 4.Conditional Rendering & List Management
  5. 5.Event Handling & Form Management
  6. 6.React Router for SPA Navigation
  7. 7.Context API for Global State
  8. 8.Working with Refs & the DOM
  9. 9.Advanced Component Patterns: HOCs & Render Props
  10. 10.Building Reusable Logic with Custom Hooks
  11. 11.Performance Optimization with Memoization
  12. 12.Error Boundaries for Robust UIs
  13. 13.Concurrent React & Suspense
  14. 14.Testing React Components with RTL
  15. 15.Advanced State Management (Overview)
React Interview Essentials
13

React · Lesson 13 of 15 · 4 min

Concurrent React & Suspense

This topic signals you've moved beyond the basics and can reason about user experience at the framework level.

After this, you'll be able to explain precisely how React keeps an app responsive during heavy rendering work, and how it manages loading states without cluttering your components with isLoading flags.

The Problem: A Blocked Main Thread

React's original rendering model was synchronous. When a state update triggered a large re-render, React would block the main thread until the work was done. If that work took longer than about 16 milliseconds, the user would perceive lag—dropped frames, janky animations, and unresponsive inputs.

Concurrent React changes this. It makes rendering interruptible. React can start rendering an update, pause to let the browser handle something more important (like the user typing), and then resume.

The trap: Confusing concurrency with parallelism. Concurrency is about managing and switching between multiple tasks on a single thread. Parallelism is doing multiple tasks simultaneously on multiple threads. JavaScript is single-threaded; React is concurrent, not parallel.

Deferring Updates with useTransition

By default, React treats all state updates as urgent. Transitions are your tool for telling React that a specific update is not urgent and can be deferred.

The useTransition hook gives you two things: an isPending boolean and a startTransition function. Any state updates you wrap in startTransition are marked as non-urgent. React will work on them in the background, and if a more urgent update comes in (like another keystroke), it will interrupt the transition to handle the urgent work first.

The isPending flag becomes true while the transition is rendering. Use it to show loading indicators or disable UI elements without replacing the existing content. This avoids jarring loading spinners for fast updates.

import React, { useState, useTransition, useMemo } from 'react';

// Simulate a slow operation
const filterItemsSlowly = (items: string[], query: string): string[] => {
  const start = performance.now();
  while (performance.now() - start < 100) {
    // Artificial delay to simulate heavy computation
  }
  return items.filter(item => item.toLowerCase().includes(query.toLowerCase()));
};

const ALL_ITEMS = Array.from({ length: 10000 }, (_, i) => `Item ${i}`);

function SearchableList() {
  const [inputValue, setInputValue] = useState('');
  const [searchQuery, setSearchQuery] = useState('');
  const [isPending, startTransition] = useTransition();

  const filteredItems = useMemo(() => 
    filterItemsSlowly(ALL_ITEMS, searchQuery), 
    [searchQuery]
  );

  const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    // Urgent: Update the input field immediately for good UX.
    setInputValue(e.target.value);

    // Not urgent: Defer the expensive filtering logic.
    // The UI remains responsive while this runs in the background.
    startTransition(() => {
      setSearchQuery(e.target.value);
    });
  };

  return (
    <div>
      <input
        type="text"
        value={inputValue}
        onChange={handleInputChange}
        placeholder="Type to search..."
      />
      {isPending && <p>Filtering list...</p>}
      <ul>
        {filteredItems.map(item => (
          <li key={item}>{item}</li>
        ))}
      </ul>
    </div>
  );
}

In this example, typing in the input feels instant because setInputValue is urgent. The list filtering, wrapped in startTransition, happens without blocking the user's input.

Declarative Loading with <Suspense>

Suspense lets your components "wait" for something to load before they render, showing a fallback UI in the meantime. It's a declarative alternative to manual isLoading state management.

The most common use case is code-splitting with React.lazy. When you try to render a lazy-loaded component, it "suspends" by throwing a special kind of promise. The nearest <Suspense> boundary above it in the tree catches this promise and renders its fallback prop. Once the component code has loaded, React tries to render it again.

This moves loading state management out of your component and into the component tree.

In an interview, contrast this with the old way: a useEffect to fetch data, multiple useState calls for isLoading, error, and data, and conditional rendering logic inside your component. Suspense simplifies this by letting the data-fetching mechanism communicate loading state directly to React.

How Suspense Works for Data Fetching

Suspense isn't just for code-splitting. It's designed for any async operation, especially data fetching. To make a data-fetching hook work with Suspense, it must follow a specific contract:

  1. When called, it checks if the data is available.
  2. If the data is cached, it returns it.
  3. If the data is being fetched, it throws the promise that represents the in-flight request.
  4. If the fetch failed, it throws the error.

React catches what's thrown. If it's a promise, it finds the nearest <Suspense> boundary and shows its fallback. If it's an error, it finds the nearest <ErrorBoundary>. Once the promise resolves, React re-renders the component that suspended.

You won't typically implement this yourself. Libraries like React Query, Relay, and SWR have Suspense modes that handle this pattern for you.

The trap: Trying to use a traditional useEffect-based fetch inside a <Suspense> boundary. It won't work. The component must throw a promise to trigger the fallback.

Where Suspense and Error Boundaries Meet

A common point of confusion is the relationship between Suspense and Error Boundaries. They are not interchangeable. They solve two different problems.

  • <Suspense> handles the pending state of an async operation (a thrown promise).
  • <ErrorBoundary> handles errors that occur during render or after an async operation fails (a thrown error).

You almost always want to wrap a Suspense boundary with an Error Boundary. If the promise for your data resolves but the data is invalid, or if the network request fails, the data-fetching hook will throw an error. Suspense won't catch it, but the Error Boundary will.

Try it

Refactor a component that uses useEffect and an isLoading state to fetch data. Use a mock, Suspense-enabled data fetching function that throws a promise (you can find simple "resource" wrappers online or write a small one). Wrap the component in <Suspense> and an <ErrorBoundary> and see how the loading and error logic moves out of your component.

Next: We'll cover how to verify your components work as expected in the next lesson, Testing React Components with RTL.

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Error Boundaries for Robust UIs

Next

Testing React Components with RTL