How to Use React Suspense to Improve your React Projects ?
Last Updated :
15 Mar, 2024
Suspense is a React component used to suspend the rendering process to perform some asynchronous tasks like getting data from API or loading any other component lazily. In simple words, Suspense is like waiting for something to happen, where we are not sure when it will happen. Suspense is beneficial in code splitting and data fetching processes.
When Suspense suspends the rendering process, for that period we can show fallback UI like loading spinner or skeleton loading. In this article, we'll see an example where Suspense is beneficial.
Code Splitting:
In Code Splitting, we can dynamically load files when required or data when required. In our case, we can load feeds, friends, and other pages only when we need them. So when we are loading files dynamically since this is an asynchronous task and takes time. So here comes the concept of Suspense, where we tell react that there is some code that will come in near future so for the time being show user some fallback UI.
Syntax:
<Suspense fallback={// show until component loads}>
// component to load
</Suspense>
If you're confused, don't worry. Let's setup our project and then we will see code splitting using Suspense in practical.
Steps to Create React Application:
Create a React application and Navigate to the project directory the following command:
npx create-react-app my-project
cd my-project
Project Structure:
Project StructureThe updated dependencies in package.json file will look like.
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^9.1.0",
"redux": "^5.0.1"
}
Example: Below is the code example:
JavaScript
// index.js
import React, {
Suspense,
lazy
} from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import "./index.css";
import {
RouterProvider,
createBrowserRouter
} from "react-router-dom";
// lazy loading components
const Home = lazy(() => import("./Home.jsx"));
const Feed = lazy(() => import("./Feed.jsx"));
const Friends = lazy(() => import("./Friends.jsx"));
const router = createBrowserRouter([
{
path: "/",
element: <App />,
children: [
{
path: "/",
element: (
// shows Loading... until Home loads
<Suspense fallback={
<h1>
Loading...
</h1>
}>
<Home />
</Suspense>
),
},
{
path: "/feed",
element: (
// shows Loading... until Feedback loads
<Suspense fallback={
<h1>
Loading...
</h1>
}>
<Feed />
</Suspense>
),
},
{
path: "/friends",
element: (
// shows Loading... until Friends loads
<Suspense fallback={
<h1>
Loading...
</h1>
}>
<Friends />
</Suspense>
),
},
],
},
]);
ReactDOM.createRoot(document.getElementById("root")).render(
<RouterProvider router={router}>
<App />
</RouterProvider>
);
JavaScript
// NavBar.js
import {
NavLink
} from 'react-router-dom';
function Navbar() {
return (
<nav>
<ul>
<li><NavLink to="/">
Home
</NavLink></li>
<li><NavLink to="/feed">
Feed
</NavLink></li>
<li><NavLink to="/friends">
Friends
</NavLink></li>
</ul>
</nav>
);
}
export default Navbar;
Steps to run the Application:
npm start
Output:
Output
Similar Reads
How To Use Single Responsibility Principle in ReactJS? If you're a developer then surely you might have listened to SOLID Principles word several times in your programming career. In Software development, the SOLID principle works as a guideline for developers. It doesn't matter which language you're using in your project, to make your code clean and ma
6 min read
Top 5 Skills You Must Know Before You Learn ReactJS Do you know enough javascript before jumping into React??Do you know how to use the map() method to loop through an array in javascript or ReactJS??If you are learning React and you get stuck on these kinds of above questions then definitely you are making mistakes in your learning process. There is
8 min read
Top 10 tricks to make your React App Faster React is one of the most popular, efficient, and powerful open-source JavaScript libraries for building dynamic and interactive user interfaces. It has the capability to build a large-scale application. But when the app grows it becomes very important to optimize the codebase so that it can work fas
5 min read
90+ React Projects with Source Code [2025] React, managed by Facebook and a vibrant community of developers and companies, is a JavaScript library designed to craft dynamic user interfaces. It empowers developers with concepts like React web apps, components, props, states, and component lifecycles. With a primary focus on building single-pa
12 min read
Best ways to Structure React Components Structuring your React applications is critical for ensuring maintainability, scalability, and code readability as a software developer. In this tutorial, we'll delve into the best practices for organizing a React app, offering examples and code snippets along the way. Make sure you have the latest
3 min read
Environment Protection Website using React Imagine building a website that champions environmental protection. That's exactly what we're aiming for with this project. We're crafting a space using React where people can learn, share, and engage in all things related to safeguarding our planet.Output Preview: Let us have a look at how the fina
7 min read