Home > ReactJS > How to Use Bootstrap Modal Popup in ReactJS Using React-Bootstrap?

How to Use Bootstrap Modal Popup in ReactJS Using React-Bootstrap?

As you know bootstrap is a CSS framework to design the layouts of websites or web applications. It has many features like a navbar, cards, buttons, accordion, badges, breadcrumb, carousel, modal, etc. You can easily copy the bootstrap code and paste it into your code. But make sure you have embedded the required CDN libraries for this.

So in this tutorial, we will learn how to use the bootstrap modal component in reactjs to make a popup with the help of the react-bootstrap package.

Bootstrap modals place a transparent or darkened background by default over all elements behind the modal popup. You can set up the position of modal with special classes and you can also use your own custom CSS.

So when you are making a react web application then you may want to use the bootstrap library, so you can easily use it. You can also use the direct bootstrap CDN links in the index.html file in the /public folder but in this tutorial, we will work with a react-bootstrap package and use it to make a popup modal in reactjs application.

So, let’s start making a bootstrap popup modal in react application using the react-bootstrap package. But before starting, make sure you have successfully installed and set up the react application, you can check the beginner’s guide for installing react application.

Create a React Bootstrap Modal

Let’s create a new component in the /src folder as PostInfo.js, you can take any name. Add the following structure code of react function component.

Now, we will install the bootstrap and react-bootstrap packages to use it to make a popup modal in reactjs component. So use the following command to install both packages.

Now we have to import these packages in our file where we want to create the bootstrap modal. You have to import the bootstrap.min.css file and the modal component of a react-bootstrap package.

In the above code, you can see we imported the bootstrap and react-bootstrap package to make a popup modal. We also imported the <Modal> and <Button> components of react-bootstrap. There are more components of this package you can check here.

But for this tutorial, we only used <Modal> and <Button> components. The <Modal> component have header, title, body and footer section as <Modal.Header>, <Modal.Title>, <Modal.Body>, and <Modal.Footer>. So we used theme accordingly, these also have further attributes that you can use them as per your requirements.

So how it basically works???

It is very simple to understand how it works. So firstly you can see it we have imported the useState react hook to update the state when action trigger.

Then we have to arrow functions handleClose() and handleShow() to handle the state of modal open and close trigger. You can see we have trigger these functions onclick events of modal open and close button.

When you click on the “Open Modal” button then handleShow() function will trigger and it will update the setShow state as true. And modal will popup to you.

And when you click on the “Close” and “Submit” button then handleClose() function will trigger and it will update the setShow state as false and close the popup modal.

You can make any action and trigger an event based on your action. So that’s how it actually works.

There is more customization that you can implement on your bootstrap modal. Customizations like you want the popup modal small, big large size, or vertically aligned. We will see an example of these customizations.

React Bootstrap Small Popup Modal

Let’s suppose you want a small popup modal, so you just have to make small changes in your <Modal> component. You need to add a size attribute as a bootstrap “sm” break-point class.

See the following code as a reference.

React Bootstrap Large Popup Modal

Now, if you want a big large popup modal as per requirements then same as above example, you just have to add the bootstrap “lg” class of size attribute in <Modal> component section.

See the following code as a reference.

React Bootstrap Vertically Align Popup Modal

As you know the default bootstrap modal position is at the top. So let’s suppose you want it to vertically center align. For this, you just need to add the centered attribute in <Modal> component section without any value.

You can see the following code as a reference example.

So these are examples of the react-bootstrap modal.

So you learned about how to use bootstrap and create a popup modal in reactjs component using the react-bootstrap package. If you like it please share it with your friends on social media and if you have queries please ask me in the comment section, I’ll help you with that.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment