React Suspense is a powerful feature that allows developers to handle asynchronous rendering gracefully by displaying fallback UI while components or data are loading. It works hand-in-hand with React.lazy, Concurrent Rendering, and Data Fetching solutions like React Query, Relay, or custom implementations.
import React, { Suspense } from "react"; const LazyComponent = React.lazy(() => import("./LazyComponent")); function App() { return ( <Suspense fallback={<div>Loading...</div>}> <LazyComponent /> </Suspense> ); } export default App;
import React from "react"; import HeavyComponent from "./HeavyComponent"; function App() { return ( <div> <h1>Main App</h1> <HeavyComponent /> </div> ); } export default App;
The HeavyComponent loads synchronously, increasing the initial load time.
import React, { Suspense } from "react"; const HeavyComponent = React.lazy(() => import("./HeavyComponent")); function App() { return ( <div> <h1>Main App</h1> <Suspense fallback={<div>Loading Heavy Component...</div>}> <HeavyComponent /> </Suspense> </div> ); } export default App;
Now, the HeavyComponent loads only when required, showing a fallback during the loading process.
import React, { Suspense } from "react"; const ComponentA = React.lazy(() => import("./ComponentA")); const ComponentB = React.lazy(() => import("./ComponentB")); function App() { return ( <div> <h1>Main App</h1> <Suspense fallback={<div>Loading Components...</div>}> <ComponentA /> <ComponentB /> </Suspense> </div> ); } export default App;
The fallback UI is displayed until both components finish loading.
React Suspense is also useful for asynchronous data fetching when integrated with libraries like Relay or React Query.
import React, { Suspense } from "react"; import { useQuery } from "react-query"; function DataComponent() { const { data } = useQuery("fetchData", fetchData); return <div>{data}</div>; } function App() { return ( <Suspense fallback={<div>Loading data...</div>}> <DataComponent /> </Suspense> ); } export default App;
If an error occurs during the loading of components or data, React Suspense does not provide built-in error handling. Use an Error Boundary for this purpose.
import React, { Suspense } from "react"; const LazyComponent = React.lazy(() => import("./LazyComponent")); function App() { return ( <Suspense fallback={<div>Loading...</div>}> <LazyComponent /> </Suspense> ); } export default App;
You can nest multiple Suspense components for granular control over fallback states.
import React from "react"; import HeavyComponent from "./HeavyComponent"; function App() { return ( <div> <h1>Main App</h1> <HeavyComponent /> </div> ); } export default App;
React Suspense is a versatile tool for managing asynchronous rendering in React applications. It simplifies handling loading states, enhances performance with lazy loading, and creates a smoother user experience when combined with code splitting and error handling techniques.
The above is the detailed content of How to Use Suspense in React for Improved Asynchronous Rendering. For more information, please follow other related articles on the PHP Chinese website!