Tutorials/React/GET STARTED
Lesson

React Suspense

GET STARTED/React

React Suspense

React Suspense is a feature that allows components to wait (suspend rendering) until something is ready (like code or data).

While waiting, it shows fallback UI (loading state).


Why Use Suspense?

In real applications:

  • Components take time to load

  • Data fetching takes time

  • Large bundles slow down UI

Suspense helps by:
โœ” Showing a loading UI
โœ” Improving user experience
โœ” Handling async rendering smoothly


Common Use Cases

  1. Lazy loading components (React.lazy)

  2. Data fetching (with frameworks like Next.js, Relay, etc.)

  3. Code splitting (performance optimization)

Basic Usage

plaintext
import { Suspense } from "react";

<Suspense fallback={<div>Loading...</div>}>
  <MyComponent />
</Suspense>

๐Ÿ”น How It Works

  • Component is loading

  • Suspense shows โ†’ fallback

  • Once ready โ†’ actual component renders

Example (Basic)

plaintext
import { Suspense } from "react";
import Fruits from "./Fruits";

function App() {
  return (
    <div>
      <Suspense fallback={<div>Loading...</div>}>
        <Fruits />
      </Suspense>
    </div>
  );
}

If Fruits takes time โ†’ "Loading..." will show

Suspense with Lazy Loading

Step 1: Import lazy

plaintext
import { lazy } from "react";

Step 2: Load component dynamically

plaintext
const Cars = lazy(() => import("./Cars"));

Step 3: Wrap with Suspense

plaintext
import { Suspense, lazy } from "react";

const Cars = lazy(() => import("./Cars"));

function App() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <Cars />
    </Suspense>
  );
}

How lazy() Works

  • Loads component only when needed

  • Reduces initial bundle size

  • Improves performance

Without Lazy vs With Lazy

โŒ Without Lazy

  • All components load at once

  • Bigger bundle size

  • Slower initial load

With Lazy + Suspense

  • Components load on demand

  • Smaller bundle

  • Faster app

Multiple Components Example

plaintext
import { Suspense, lazy } from "react";

const Header = lazy(() => import("./Header"));
const Sidebar = lazy(() => import("./Sidebar"));
const Content = lazy(() => import("./Content"));

function App() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <Header />
      <div style={{ display: "flex" }}>
        <Sidebar />
        <Content />
      </div>
    </Suspense>
  );
}

One Suspense can handle multiple components


Key Points

  • Suspense shows fallback UI while loading

  • Works best with lazy()

  • Improves performance (code splitting)

  • Makes UX smoother


Interview Questions

  1. What is React Suspense?

  2. What is fallback in Suspense?

  3. Difference between lazy loading and normal import?

  4. How does Suspense improve performance?

  5. Can Suspense handle multiple components?


๐Ÿงพ Summary

  • Suspense โ†’ loading UI handler

  • fallback โ†’ what user sees while loading

  • lazy() โ†’ dynamic import

  • Used for โ†’ performance + better UX

React Suspense | React | Softcrayons Tech Solutions