How to Alert Users to Errors with React Bootstrap ?
Last Updated :
28 Apr, 2025
In React Bootstrap, an `Alert` is a component used to display various types of messages or notifications to users. These messages can include informational messages, warnings, errors, or any other kind of feedback that you want to communicate to the user. Alerts are commonly used to provide feedback or to convey important information in a visually prominent and user-friendly manner.
Steps to create the application
Step 1: Install React Application
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command
cd foldername
Step 3: Install the required modules using the following command.
npm install react-bootstrap bootstrap
Here are two common approaches to alert users to errors with React Bootstrap:
Approach 1: Using the Alert Component
In this approach,
- variant="danger" sets the alert to display with a red background for error messages.
- onClose is used to define an action when the close button is clicked, typically to hide the alert.
- dismissible adds a close button to the alert.
Syntax:
<Alert variant="danger" onClose={() => setShow(false)} dismissible>
<Alert.Heading>Error</Alert.Heading>
<p>Error message goes here.</p>
</Alert>
Example: This example shows the use of the above-explained appraoch.
JavaScript
import React, { useState } from "react";
import { Alert, Button } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
function ErrorAlertExample() {
const [show, setShow] = useState(false);
const handleButtonClick = () => {
setShow(true);
};
return (
<div>
<Button onClick={handleButtonClick}>
Show Error
</Button>
{show && (
<Alert
variant="danger"
onClose={() => setShow(false)}
dismissible>
<Alert.Heading>Error</Alert.Heading>
<p>Error message goes here.</p>
</Alert>
)}
</div>
);
}
function App() {
return (
<div
className="col-12 d-flex
justify-content-center
align-items-center"
style={{ height: "100vh" }}>
<div className="col-4 h-50">
<ErrorAlertExample />
</div>
</div>
);
}
export default App;
Steps To Run Application:
npm start
Output:

Approach 2: Using the Toast Component
In this approach,
- position defines the location where the toast notification appears.
- bg sets the background color for error messages.
- autohide specifies the duration in milliseconds before the toast automatically disappears.
Syntax
<ToastContainer position="top-end">
<Toast bg="danger" autohide={3000}>
<Toast.Header>
<strong className="me-auto">Error</strong>
</Toast.Header>
<Toast.Body>Error message goes here.</Toast.Body>
</Toast>
</ToastContainer>
Example: This example shows the use of the above-explained approach.
JavaScript
import React, { useState } from "react";
import {
Button,
Toast,
ToastContainer,
} from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.min.css";
function ErrorToastExample() {
const [showA, setShow] = useState(false);
const toggleShow = () => setShow(!showA);
return (
<div className="relative">
<Button onClick={toggleShow}>Show Error</Button>
<ToastContainer>
<Toast
bg="danger"
autohide={100}
show={showA}
onClose={toggleShow}>
<Toast.Header>
<strong className="me-auto">
Error
</strong>
</Toast.Header>
<Toast.Body>
Error message goes here.
</Toast.Body>
</Toast>
</ToastContainer>
</div>
);
}
function App() {
return (
<div
className="col-12 d-flex
justify-content-center
align-items-center"
style={{ height: "100vh" }}>
<div className="col-4 h-50">
<ErrorToastExample />
</div>
</div>
);
}
export default App;
Steps To Run Application:
npm start
Output:
output
Similar Reads
How to Use Bootstrap with React? Bootstrap is one of the most popular front-end frameworks, widely used to create visually appealing, responsive, and mobile-first websites quickly. It provides pre-designed UI components, grid systems, and various CSS classes that help you build mobile-first, responsive web applications quickly and
9 min read
How to Install and Use React Bootstrap with Gatsby JS? In this article, we are going to learn how to install and use React Bootstrap with Gatsby JS. We will install GatsbyJS, then we will install React-Bootstrap in the project to use the class and components of Bootstrap. PrerequisitesGatsbyReactReact-BootstrapApproachTo begin, we'll install the Gatsby
2 min read
How to Add Dismissible Alerts using React Bootstrap ? A dismissible alert is a type of message that can be dismissed/canceled by the user by clicking the close icon(X) or close button. React Bootstrap provides us with an Alert react component for this. The react component has a dismissible prop which can be set to true to make the alert dismissible.Cre
2 min read
How to display error without alert box using JavaScript ? JavaScript errors are displayed using alert boxes, which can interrupt user experience and feel outdated. But Now, there are many alternatives to alert boxes for displaying error messages in a more user-friendly way. This article covers different methods to display errors dynamically without using a
3 min read
How To Reset a React-Bootstrap Form After Submit? Forms are one of the most useful elements in web applications. It allows users to input their credentials and submit those data. There are many types of forms present (User Registration, Login, Feedback Form etc).In this article, we will see how to reset a React-Bootstrap Form after submission.Prere
3 min read
How to use Bootstrap in React JS ? Explore the seamless integration of Bootstrap into React JS for enhanced styling and responsive web development. This comprehensive guide provides step-by-step instructions, covering the installation process, utilization of Bootstrap components, and best practices to harness the power of both techno
3 min read
How to Create Alerts in Bootstrap ? Bootstrap Alerts offer a simple method for crafting predefined messages, enhancing their appeal to users. They streamline message display, adding style for increased engagement and improved user experience by presenting important information effortlessly and appealingly.Syntax:<div class="alert a
3 min read
How to pass data to a React Bootstrap modal? In React Bootstrap, we have the styling and interactive components of Modal. In simple terms, Modal is nothing but the popup box that is opened when some action has been performed. To make the modal customizable in terms of its behavior, we can pass the custom data to the React Bootstrap modal using
4 min read
How to create warning notification alerts in Bootstrap ? Before or after performing an action, we frequently encounter specific notifications on some websites. These alert messages are highlighted text that should be taken into account when executing a task. Using preset classes in Bootstrap, these alert messages can be displayed on the website.Approach:
3 min read
React-Bootstrap Alerts Component Introduction: React-Bootstrap is a front-end framework that was designed keeping react in mind. Bootstrap was re-built and revamped for React, hence it is known as React-Bootstrap. Alerts are used to pop notifications on the screen. Depending upon the scenario the nature and theme of the alerts chan
2 min read