React Bootstrap Alerts

React Alerts - Bootstrap 4 & Material Design

Bootstrap alerts are feedback messages which are displayed after specific actions preceded by the user. Length of the text is not limited.

With the right use of colors, they add some emotional weight to our information, ranging from a simple warning to critical system failure or from an operation success to a neutral information.

Additional resources: Have a look at our tutorial about Web Push alerts


Basic examples

Alerts are available for any length of text, as well as an optional dismiss button. For proper styling, use one of the eight contextual props (e.g., color="success"). For inline dismissal, use the dismiss prop.

        
            
          import React from "react";
          import { MDBContainer, MDBAlert } from 'mdbreact';

          const AlertPage = () => {
            return (
              <MDBContainer>
                <MDBAlert color="primary" >
                  A simple primary alert—check it out!
                </MDBAlert>
                <MDBAlert color="secondary" >
                  A simple secondary alert—check it out!
                </MDBAlert>
                <MDBAlert color="success" >
                  A simple success alert—check it out!
                </MDBAlert>
                <MDBAlert color="danger" >
                  A simple success alert—check it out!
                </MDBAlert>
                <MDBAlert color="warning" >
                  A simple warning alert—check it out!
                </MDBAlert>
                <MDBAlert color="info" >
                  A simple info alert—check it out!
                </MDBAlert>
                <MDBAlert color="light" >
                  A simple light alert—check it out!
                </MDBAlert>
                <MDBAlert color="dark" >
                  A simple dark alert—check it out!
                </MDBAlert>
              </MDBContainer>
            );
          };

          export default AlertPage;
        
        
    

Conveying meaning to assistive technologies

Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (e.g. the visible text), or is included through alternative means, such as additional text hidden with the .sr-only class.



Additional content

Alerts can also contain additional HTML elements like headings, paragraphs and dividers.

        
                      
          import React from "react";
          import { MDBContainer, MDBAlert } from 'mdbreact';

          const AlertPage = () => {
            return (
              <MDBContainer>
                <MDBAlert color="success">
                  <h4 className="alert-heading">Well done!</h4>
                  <p>Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.</p>
                  <hr />
                  <p className="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
                </MDBAlert>
              </MDBContainer>
            );
          };

          export default AlertPage;        
        
        
    

Dismissing

Using the React Alert component, it’s possible to dismiss any alert inline. You can see this in action with a live demo:

        
            
          import React from "react";
          import { MDBContainer, MDBAlert } from 'mdbreact';

          const AlertPage = () => {
            return (
              <MDBContainer>
                <MDBAlert color="warning" dismiss>
                  <strong>Holy guacamole!</strong> You should check in on some of those fields below.
                </MDBAlert>
              </MDBContainer>
            );
          };

          export default AlertPage;
        
        
    

React Alerts - API

In this section you will find advanced information about the Alert component. You will find out which modules are required, what are the possibilities of configuring the component, and what events and methods you can use in working with it.


Alert import statement

In order to use Alert component make sure you have imported proper module first.

        
            
            import { MDBAlert } from "mdbreact";
          
        
    

The table below shows the configuration options of the MDBAlert component.

Name Type Default Description Example
className String Adds custom classes <MDBAlert className="myClass" ... />
color string primary Sets color of the alert. Choose one of these: 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark' <MDBAlert color="primary" ... />
dismiss Boolean false Adds a close button and lets you dismiss alert inline <MDBAlert dismiss ... />

API Reference: Alert Events

Name Parameters Description Example
onClose This event fires immediately when the close instance method is called. <MDBAlert onClose={()=> alert('onClose event')} />
onClosed This event is fired when the alert has been closed. <MDBAlert onClosed={()=> alert('onClosed event')} />