Loading content...
Loading content...
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).
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
Lazy loading components (React.lazy)
Data fetching (with frameworks like Next.js, Relay, etc.)
Code splitting (performance optimization)
plaintext
import { Suspense } from "react";
<Suspense fallback={<div>Loading...</div>}>
<MyComponent />
</Suspense>Component is loading
Suspense shows → fallback
Once ready → actual component renders
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
plaintext
import { lazy } from "react";plaintext
const Cars = lazy(() => import("./Cars"));plaintext
import { Suspense, lazy } from "react";
const Cars = lazy(() => import("./Cars"));
function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<Cars />
</Suspense>
);
}Loads component only when needed
Reduces initial bundle size
Improves performance
All components load at once
Bigger bundle size
Slower initial load
Components load on demand
Smaller bundle
Faster app
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
Suspense shows fallback UI while loading
Works best with lazy()
Improves performance (code splitting)
Makes UX smoother
What is React Suspense?
What is fallback in Suspense?
Difference between lazy loading and normal import?
How does Suspense improve performance?
Can Suspense handle multiple components?
Suspense → loading UI handler
fallback → what user sees while loading
lazy() → dynamic import
Used for → performance + better UX