Author avatar

Kimaru Thagana

React Bootstrap Modals

Kimaru Thagana

  • Sep 5, 2020
  • 8 Min read
  • 232 Views
  • Sep 5, 2020
  • 8 Min read
  • 232 Views
Web Development
Front End Web Development
Client-side Frameworks
React

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.

1
2
3
4
5
npx create-react-app react-bootstrap-modal

cd react-bootstrap-modal

npm start
bash

Include react-bootstrap in your basic React app.

1
npm install react-bootstrap bootstrap
bash

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import React, {Component} from 'react';

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

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

}


export default App;
js

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

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

You can now import bootstrap components.For example:

1
import { Modal } from 'react-bootstrap';
js

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
js

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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;
js

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.

1