Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ReactJS Reactstrap Modal Component

  • Last Updated : 29 Jul, 2021

Reactstrap is a popular front-end library that is easy to use React Bootstrap 4 components. This library contains the stateless React components for Bootstrap 4. The Modal component provides a solid foundation for creating dialogs, lightboxes, popovers, etc. We can use the following approach in ReactJS to use the ReactJS Reactstrap Modal Component.

Modal Props:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • isOpen: The modal will show itself when this is set to true.
  • autoFocus: The Modal is opened and is automatically focused on its own when this is set to true.
  • centered: It is used to make the Centered Modal.
  • size: It is used to set the Modal size
  • toggle:  It is a callback function that is triggered when the component toggles.
  • role:  It is used to denote the role attribute, its default value is “dialog”.
  • labelledBy: It is used to reference the ID of the title element in the modal.
  • keyboard: It is used to indicate whether to support press ESC to close or not.
  • backdrop: The Modal will display the background in its opened state when this is set to true.
  • scrollable: It is used to allow the modal to be scrollable when content is long.
  • external: It is used to allows for a node or a component to exist next to the modal.
  • onEnter: It is a callback function that is called on componentDidMount event.
  • onExit: It is a callback function that is called on componentWillUnmount event.
  • onOpened: It is a callback function that is called when transitioning in is done.
  • onClosed: It is a callback function that is called when transitioning out is done.
  • className: It is used to denote the class name for styling.
  • wrapClassName: It is used to pass the class name of the container of the modal dialog. 
  • modalClassName: It is used to add an optional extra class name for the modal class.
  • backdropClassName: It is used to add an optional extra class name to .modal-backdrop.
  • contentClassName: It is used to add an optional extra class name to .modal-content  
  • fade: It is used to indicate whether the fade transition occurs or not.
  • cssModule: It is used to denote the CSS module for styling.
  • zIndex: It is used to denote the z-index of the Modal.
  • backdropTransition: It is used for the backdrop Transition as it controls backdrop transition.
  • modalTransition: It is used for the modal Transition as it controls modal transition.
  • innerRef: It is used to denote the inner ref element for this component.
  • unmountOnClose: It is used to unmount from DOM after closing the modal.
  • returnFocusAfterClose: It is used to return the focus back to the element which opens the modal after the modal closes.
  • container: It is the container attribute, and it is of type any.
  • trapFocus: It is used to manage the focus for its descendants.

Creating React Application And Installing Module:

Step 1: Create a React application using the following command:



npx create-react-app foldername

 

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Step 3: After creating the ReactJS application, Install the required module using the following command:

npm install reactstrap bootstrap

Project Structure: It will look like the following.

Project Structure

Example 1: Now write down the following code in the App.js file. Here we have shown the Modal with a delay of 2 seconds and we have shown Modal without ModalHeader and ModalFooter.

Javascript




import React from 'react'
import 'bootstrap/dist/css/bootstrap.min.css';
import {
    Button, Modal, ModalFooter,
    ModalHeader, ModalBody
} from "reactstrap"
  
function App() {
  
    // Modal open state
    const [modal, setModal] = React.useState(false);
  
    // Toggle for Modal
    const toggle = () => setModal(!modal);
  
    return (
        <div style={{
            display: 'block', width: 700, padding: 30
        }}>
            <h4>ReactJS Reactstrap Modal Component</h4>
            <Button color="primary"
                onClick={toggle}>Open Modal</Button>
            <Modal isOpen={modal}
                toggle={toggle}
                modalTransition={{ timeout: 2000 }}>
                <ModalBody>
                    Simple Modal with just ModalBody...
                </ModalBody>
            </Modal>
        </div >
    );
}
  
export default App;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:



Example 2: Now write down the following code in the App.js file. Here we have shown the Modal without any delay and we have shown Modal with ModalHeader and ModalFooter.

Javascript




import React from 'react'
import 'bootstrap/dist/css/bootstrap.min.css';
import {
    Button, Modal, ModalFooter,
    ModalHeader, ModalBody
} from "reactstrap"
  
function App() {
  
    // Modal open state
    const [modal, setModal] = React.useState(false);
  
    // Toggle for Modal
    const toggle = () => setModal(!modal);
  
    return (
        <div style={{
            display: 'block', width: 700, padding: 30
        }}>
            <h4>ReactJS Reactstrap Modal Component</h4>
            <Button color="danger"
                onClick={toggle}>Click me to open Modal</Button>
            <Modal isOpen={modal} toggle={toggle}>
                <ModalHeader
                    toggle={toggle}>Sample Modal Title</ModalHeader>
                <ModalBody>
                    Sample Modal Body Text to display...
                </ModalBody>
                <ModalFooter>
                    <Button color="primary" onClick={toggle}>Okay</Button>
                </ModalFooter>
            </Modal>
        </div >
    );
}
  
export default App;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Reference: https://reactstrap.github.io/components/modals/




My Personal Notes arrow_drop_up
Recommended Articles
Page :