React Bootstrap Modal Example

In this tutorial, we will show you how to install & integrate Bootstrap library into a React JS app and how to create a Bootstrap modal popup.

How to Create Bootstrap Modal in React JS

Steps to create a bootstrap modal in React JS app:

Step 1 – Create React App

Run the following command on your terminal to create a new react app:

npx create-react-app my-react-app

Run the following command on your terminal:

npm start

Check out your React app on this URL: localhost:3000

Step 2 – Install React Bootstrap

Run the following command to install the react bootstrap library into your react app:

npm install bootstrap --save

npm install react-bootstrap bootstrap

Add bootstrap.min.css file in src/App.js file:

import React, { Component } from 'react'
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
function App() {
  return (
    <div>
      <h2>How to Create Bootstrap Modal Popup in React</h2>
    </div>
  );
}
export default App;

Step 3 – Create Bootstrap Modal Component

Create BootstrapModalComponent.js file in src directory, and create a popup modal using bootstrap:

import React from 'react'
import { Button,Modal } from 'react-bootstrap'
class BootstrapModalComponent extends React.Component{
    constructor(){
        super();
        this.state = {
            showHide : false
        }
    }
    handleModalShowHide() {
        this.setState({ showHide: !this.state.showHide })
    }
    render(){
        return(
            <div>
                <Button variant="primary" onClick={() => this.handleModalShowHide()}>
                    Launch demo modal
                </Button>
                <Modal show={this.state.showHide}>
                    <Modal.Header closeButton onClick={() => this.handleModalShowHide()}>
                    <Modal.Title>Modal heading</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
                    <Modal.Footer>
                    <Button variant="secondary" onClick={() => this.handleModalShowHide()}>
                        Close
                    </Button>
                    <Button variant="primary" onClick={() => this.handleModalShowHide()}>
                        Save Changes
                    </Button>
                    </Modal.Footer>
                </Modal>
            </div>
        )
    }

}
export default BootstrapModalComponent;

Step 4 – Import Bootstrap Modal Component in App.js

In this step, you need to import BootstrapModalComponent.js file in src/App.js file:

import React from 'react';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import BootstrapModalComponent from './BootstrapModalComponent'
function App() {

  return (
    <div className="App">
      <BootstrapModalComponent />
    </div>
  );
}
export default App;

Conclusion

In this tutorial, you have learned how to integrate the bootstrap modal in react js app using bootstrap libraries.

Recommended React JS Posts

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *