Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

React Bootstrap Modals

Sep 5, 2020 • 8 Minute Read

Introduction

In frontend development, developers have to build the user interface (UI) with user experience (UX) in mind. Modals are window components that allow an app to display user notifications, dialogues, or custom content in an overlayed window above the main one. They are ideal in terms of communicating notifications and dialogues to users during. This enriches the UX by enabling a cleaner and more intuitive UI.

Say you are a React developer working on an exciting project for your startup: a fintech product that is web-based. As the developer, you are mainly required to build a simple and intuitive UI that also has a smooth UX. A particular page on the site is performing a sensitive action, fund transfer. The user has to be really sure that they want to do this. To ask for the user's confirmation, you decide to develop a notification and dialogue component that will inform the user that they are about to perform a sensitive action and ask if they wish to cancel or proceed. The component will be a modal.

In this guide, you will learn how to develop a bootstrap modal that would fit this requirement using React.js. The guide assumes that you have basic knowledge of React.js (at least beginner level).

Set Up a Sample App

Open your terminal and run these commands to create a basic React app.

      npx create-react-app react-bootstrap-modal

cd react-bootstrap-modal

npm start
    

Include react-bootstrap in your basic React app.

      npm install react-bootstrap bootstrap
    

Delete the code in the newly created app.js file and paste the sample code below.

      import React, {Component} from 'react';

class App extends Component{
    constructor(props) {
        super(props);
        
    }

    render() {
        return (
            <div>
                
            </div>
        );
    }

}


export default App;
    

In your app.js file, include the stylesheet as well.

      import 'bootstrap/dist/css/bootstrap.min.css';
    

You can now import bootstrap components.For example:

      import { Modal } from 'react-bootstrap';
    

Set Up the Modal

In the sample code below, you will create a simple form that, when the user submits, will show a confirmation modal. Both the form and button are React Bootstrap components. Copy and paste the sample code below.

      import React, {Component} from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import {Row, Button,Col, Form, Card} from "react-bootstrap";

class App extends Component{
    constructor(props) {
        super(props);

    }

    render() {
        return (
            <Row className="justify-content-md-center">
                <Col lg={4}>

                    <Card style={{ width: '20rem' }}>
                        <Card.Body>
                            <Card.Text>
                                <Form>
                                    <Form.Group controlId="formBasic">
                                        <Form.Label>Transfer Funds</Form.Label>
                                        <Form.Control type="Name" placeholder="Enter Name" />
          
                                    </Form.Group>

                                    <Form.Group controlId="formBasicPassword">
                                        <Form.Label>Amount</Form.Label>
                                        <Form.Control type="number" placeholder="Amount" />
                                    </Form.Group>

                                    <Button variant="primary" type="submit">
                                        Submit
                                    </Button>
                                </Form>
                            </Card.Text>
                        </Card.Body>
                    </Card>
                </Col>
            </Row>
        );
    }
}
export default App;
    

The code above illustrates a simple form used to input details. Once the submit button is clicked, a confirmation modal will pop up and the user will be asked to confirm the transaction.

Set up the modal. In this case, you will render the modal in a separate function. Copy and paste the code below.

      import React, {Component} from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
import {Modal, Row, Button,Col, Form, Card} from "react-bootstrap";

class App extends Component{
    constructor(props) {
        super(props);
        this.state = ({
            show: false,
        })

        this.handleClose = this.handleClose.bind(this);
        this.handleShowModal = this.handleShowModal.bind(this);
    }

    handleShowModal(){
        this.setState({
            show: true
        })
    }

    handleClose(){
        this.setState({
            show:false
        })
    }

    renderModal() {

        return (
            <Modal show={this.state.show} onHide={this.handleClose} backdrop="static">
                <Modal.Header closeButton>
                    <Modal.Title>Confirm Transaction</Modal.Title>
                </Modal.Header>
                <Modal.Body>You are about to transfer funds to John Doe. Do you wish to continue?</Modal.Body>
                <Modal.Footer>
                    <Button variant="primary" onClick={this.handleClose}>
                        Proceed
                    </Button>
                    <Button variant="danger" onClick={this.handleClose}>
                        Cancel
                    </Button>
                </Modal.Footer>
            </Modal>
        )

    }

    render() {
        return (
            <Row className="justify-content-md-center">
                <Col lg={4}>
                    <Card style={{ width: '20rem' }}>
                        <Card.Body>
                            <Card.Text>
                                <Form>
                                    <Form.Group controlId="formBasicName">
                                        <Form.Label>Transfer Funds</Form.Label>
                                        <Form.Control type="name" placeholder="Enter Name" />
                                    </Form.Group>
                                    <Form.Group controlId="formBasicAmount">
                                        <Form.Label>Amount</Form.Label>
                                        <Form.Control type="number" placeholder="Amount" />
                                    </Form.Group>
                                    <Button variant="primary"
                                            onClick={this.handleShowModal}
                                    >
                                        Transfer
                                    </Button>
                                </Form>
                            </Card.Text>
                        </Card.Body>
                    </Card>
                </Col>

                <Col>
                    {this.renderModal()}
                </Col>
            </Row>
        );
    }
}

export default App;
    

In the example above, the rendermodal() function renders your modal. In the constructor, you initialize a state called show. This state is false when the page loads. This state will be responsible for showing your modal. Once the submit button is clicked, the handleShowModal() function sets your state to true and the modal is shown. The handleClose() is responsible for hiding your modal and sets your show state to false. For good practice, the sample renders the modal in a separate function. Also, to ensure the user doesn't ignore the modal or to enforce modal focus, you can add backdrop="static".

Conclusion

In this guide, you have developed a bootstrap modal using React.js. This knowledge is vital to developers in frontend and React developer positions.

To further build on this guide, the next challenge would be to develop a modal using custom hooks and a context API. A resource to get you started can be found here.