How to add Spinner while render a component on react-bootstrap?
Last Updated :
24 Jul, 2024
In this article, we will add Spinner while rendering a component with the help of react-bootstrap. In the react web application, we can enhance the user experience by adding a Spinner styling component from the react-bootstrap library.
The two ways to add a spinner while rendering are discussed below:
Steps to create React Application and installing Bootstrap:
Step 1: Create a React application using the following command:
npx create-react-app multi-select
Step 2: After creating your project folder(i.e. multi-select), move to it by using the following command:
cd multi-select
Step 3: Install react-bootstrap in your working directory i.e. multi-select by executing the below command in the VScode terminal:
npm install react-bootstrap
npm install bootstrap
Step4: Add Bootstrap CSS to the index.js file:
import 'bootstrap/dist/css/bootstrap.min.css';
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"bootstrap": "^5.3.2",
"react": "^18.2.0",
"react-bootstrap": "^2.9.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Approach 1: Using Promises
Example 1: In this example, we're using the React Bootstrap Spinner Component to show a loading spinner when data is being fetched. When the user clicks the "Load Data" button, we set a loading state and display the spinner. The data is fetched asynchronously using a promise, and once it's available, we turn off the loading state, hide the spinner, and show the data in a list.
JavaScript
import React, { useState, useEffect } from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import { Spinner, Button, Container } from "react-bootstrap";
function App() {
const [loading, setLoading] = useState(false);
const [data, setData] = useState([]);
const [showSpinner, setShowSpinner] = useState(false);
useEffect(() => {
if (loading) {
setShowSpinner(true);
fetchData().then((result) => {
setData(result);
setLoading(false);
setShowSpinner(false);
});
}
}, [loading]);
async function fetchData() {
return new Promise((resolve) => {
setTimeout(() => {
resolve([
`Company Profile and Brand:`,
`GeeksforGeeks is a leading platform for
computer science resources and coding challenges.`,
`Founded in 2009 by Sandeep Jain, the platform
has become a trusted source for developers, students, and
professionals`,
`It offers a wide range of tutorials, practice problems,
articles, and courses in various programming languages and
technologies`,
`GeeksforGeeks is known for its competitive programming
challenges and coding contests.`,
`The platform has a strong community of contributors and
learners who help each other grow their skills`,
`Whether you are a beginner or an experienced developer,
GeeksforGeeks has resources to suit your needs.`,
`Join the GeeksforGeeks community today and accelerate
your learning journey!`,
]);
}, 2000);
});
}
return (
<Container className="mt-4">
{" "}
<div className="jumbotron text-center bg-light">
{" "}
<h1 className="text-success">
GeeksforGeeks
</h1>
{" "}
<p>
{" "}
A leading platform for computer science
resources and coding challenges.
{" "}
</p>
{" "}
<Button
variant="primary"
onClick={() => setLoading(true)}
disabled={loading}>
{" "}
{loading ? "Loading..." : "Load Data"}{" "}
</Button>
{" "}
</div>
{" "}
{showSpinner && (
<div className="text-center mt-4">
{" "}
<Spinner animation="border" role="status">
{" "}
<span className="visually-hidden">Loading...</span>
{" "}
</Spinner>
{" "}
</div>
)}
{" "}
{data.length > 0 && (
<ul className="list-group mt-4">
{" "}
{data.map((item, index) => (
<li key={index} className="list-group-item">
{" "}
{item}
{" "}
</li>
))}
{" "}
</ul>
)}
{" "}
</Container>
);
}
export default App;
Steps to run the application:
Step 1: Run the application using the following command from the root directory of the project:
npm start
Step 2: Now open your browser and go to http://localhost:3000/, you will see the following output.
Output:
Approach 2: Using Conditional Rendering
Example : This example uses Conditional Rendering to determine what to display based on the value of the loading state variable. When the button of Load Data is been clicked, the loading state is set to true, which causes the Spinner to be displayed.
JavaScript
import "bootstrap/dist/css/bootstrap.min.css";
import React, { useState, useEffect } from "react";
import "bootstrap/dist/css/bootstrap.css";
import { Spinner, Button, Container } from "react-bootstrap";
function App() {
const [loading, setLoading] = useState(false);
const [data, setData] = useState([]);
useEffect(() => {
const fetchData = async () => {
setTimeout(() => {
setData([
"Company Profile and Brand:",
"GeeksforGeeks is a leading platform...",
]);
setLoading(false);
}, 2000);
};
if (loading) {
fetchData();
}
}, [loading]);
return (
<Container className="mt-4">
{" "}
<div className="jumbotron text-center bg-light">
{" "}
<h1 className="text-success">
GeeksforGeeks
</h1>
{" "}
<p>
Using Conditional Rendering
</p>
{" "}
<Button
variant="primary"
onClick={() => setLoading(true)}
disabled={loading}>
{" "}
{loading ? "Loading..." : "Load Data"}
{" "}
</Button>
{" "}
</div>{" "}
{loading ? (
<div className="text-center mt-4">
{" "}
<Spinner animation="border" role="status">
{" "}
<span className="visually-hidden">
Loading...
</span>
{" "}
</Spinner>{" "}
</div>
) : (
data.length > 0 && (
<ul className="list-group mt-4">
{" "}
{data.map((item, index) => (
<li key={index} className="list-group-item">
{" "}
{item}
{" "}
</li>
))}{" "}
</ul>
)
)}
{" "}
</Container>
);
}
export default App;
Output:
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read