How to show alerts in React Bootstrap

Key takeaways:

  • React Bootstrap simplifies the process of displaying alerts with the built-in Alert component.

  • Install React Bootstrap using npm to get started.

  • Use the useState Hook to manage the visibility of alerts.

  • The variant prop customizes the alert’s style, like “success” or “danger.”

  • The dismissible prop allows users to close the alert.

  • Alerts can be shown or hidden based on a button click and state management.

When developing web applications, it’s often essential to provide feedback or special instructions to users through alerts. React Bootstrap simplifies this process with its Alert component, allowing us to display various types of alerts in a straightforward manner. In this Answer, we’ll walk through the steps to implement alerts in our React application using React Bootstrap.

In React Bootstrap, we can use the Alert component to display alerts.

Prerequisites

Before we begin, make sure we have React Bootstrap installed. We can install it using npm:

npm install react-bootstrap bootstrap

Import the Alert component

First, we’ll import the necessary components in our React component:

import React, { useState } from 'react';
import Alert from 'react-bootstrap/Alert';

Managing Alert visibility with state

We’ll need a state variable to control the visibility of the alert. Here’s how to set it up:

const MyComponent = () => {
const [showAlert, setShowAlert] = useState(false);
const handleShowAlert = () => {
setShowAlert(true);
};
const handleCloseAlert = () => {
setShowAlert(false);
};
return (
<div>
<button onClick={handleShowAlert}>Show Alert</button>
{showAlert && (
<Alert variant="success" onClose={handleCloseAlert} dismissible>
This is a success alert!
</Alert>
)}
</div>
);
};
Structure of alert component

In this example, when the “Show Alert” button is clicked, the handleShowAlert function sets the showAlert state to true, displaying the alert. The onClose prop of the Alert component is used to handle the closing of the alert when the user clicks the close button.

The variant prop is used to specify the style of the alert. We can use values like “success,” “info,” “warning,” or “danger.” The dismissible prop makes the alert dismissible, meaning it will have a close button.

Let’s combine all the above steps and see the working example:

Note: You can try out the different types of alerts in React. Here's a list of variants you can replace success with in the above code:

  • primary

  • secondary

  • danger

  • warning

Explanation

  • Line 1: Imports the necessary modules from React to define a functional component (useState is a React hook used to manage state within functional components).

  • Line 2: Imports the Alert component from React Bootstrap. This component is used to display alerts with various styles and functionalities.

  • Line 3: Imports the Bootstrap CSS file, which provides styling for React Bootstrap components.

  • Lines 5–11: Initializes a state variable showAlert using the useState Hook. showAlert is a boolean that determines whether the alert should be displayed (false initially). handleShowAlert sets the showAlert state to true when called. This function is triggered when a button is clicked to show the alert. The const handleCloseAlert = () => { setShowAlert(false); }; defines a function handleCloseAlert that sets the showAlert state to false when called. This function is triggered when the alert’s close button is clicked, dismissing the alert.

  • Line 13: The return statement contains JSX (JavaScript XML) that represents the component’s UI.

  • Line 14: <div> wraps the content of the component.

  • Line 15: Renders a button that, when clicked, triggers the handleShowAlert function, displaying the alert.

  • Lines 17–21: Conditionally renders the <Alert> component only if showAlert is true. It uses the variant="success" prop to display a success-styled alert. The onClose={handleCloseAlert} prop assigns the handleCloseAlert function to be executed when the alert is closed. The dismissible attribute adds a close button to the alert, allowing the user to dismiss it.

This code creates a React component (MyComponent) that displays an alert when a button is clicked and provides functionality to close/dismiss the alert.

Conclusion

This simple implementation demonstrates how to effectively use the Alert component from React Bootstrap. By following these steps, you can enhance your user interface with alerts that provide valuable feedback and improve user experience.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How can we show a success message in React?

Use the Message component in React to display a success message by setting its type prop to "success".


How can we show bootstrap alerts in React?

Use the Alert component from React Bootstrap and control its visibility with state to show a Bootstrap alert in React.


How can we customize an alert box in React JS?

Customize the alert box in React by modifying the Alert component’s props, such as variant, className, or inline styles, to adjust its appearance and behavior.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved