Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Gaurav Singhal

How to Dismiss a React-Bootstrap Popover on Click Outside

Gaurav Singhal

  • Sep 15, 2020
  • 7 Min read
  • Sep 15, 2020
  • 7 Min read
Web Development
Front End Web Development
Client-side Framework


The React community has always made integrating open-source libraries easy for developers. It eliminates the unnecessary use of jQuery for Bootstrap components such as tooltips, popovers, modals, etc. by converting these components into React components. Popovers are widely used to display some additional content in an enhanced visual effect on the page. This guide explores how to dismiss a popover on clicking outside anywhere on the page.


Popovers can be used in several ways, such as calculating the price of an inventory item in an e-commerce web app, displaying details of a new car model, etc. However, when using a UI component, be sure it is conveniently handled on the user's end. Attractive UI elements seem easy to use, but their complete use case should be correctly anticipated before implementing it in code. The basic feature that any modal, tooltip, or popover should have is easy opening and closing. Since popovers are triggered using a click event through a button, closing the same popover should be more customized in terms of end-user experience.

The default dismissing action of React Bootstrap popover is the same button that triggers it. The button that opens the popover on click acts like a toggle. The user, however, expects the popover to close when they click anywhere outside the page, just like modals, due to their similar interfaces.

Set up a Popover

Create a Project

Make sure you have Nodejs and npm installed in your machine (at least version 8 or higher) along with a code editor and a web browser (preferably Chrome or Firefox).

Create a new project using create-react-app:

1npx create-react-app react-bootstrap-popover

Instal React Bootstrap

Inside the root directory run the following command to install React Bootstrap library

1npm install react-bootstrap bootstrap

This will install both Bootstrap and React Bootstrap inside the project.

Clean up the Template

For brevity, let's put all the code inside App.js . Remove the logo, App.css, and all their imports from App.js. Clean out the starter template inside the App component. Your App.js should look like this.

1import React from 'react';
3function App() {
4  return (
5    <div className="App">
6      <h2>Hello</h2>
7    </div>
8  );
11export default App;

Add the Popover Component

For regular Bootstrap styles to work correctly, import the Bootstrap styles on top.

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

Above is equivalent to adding Bootstrap CDN in your index.html file. Now import three things; Popover, OverlayTrigger and Button from react-bootstrap

1import Popover from 'react-bootstrap/Popover';
2import OverlayTrigger from 'react-bootstrap/OverlayTrigger';
3import Button from 'react-bootstrap/Button';

To create a popover inside the Popover Component, render the Popover.Title component to indicate the title of the popover and Popover.Content Component to indicate its content. Store this popover in a constant variable and output it inside the JSX template.

2 const popover = (
3    <Popover id="popover-basic">
4      <Popover.Title as="h3">Popover title</Popover.Title>
5      <Popover.Content>
6        Popover content <strong>some strong content</strong> Normal content again
7      </Popover.Content>
8    </Popover>
9  );

Output the popover inside an overlay and use the trigger prop to set the type of event the overlay listens for.

1 ....
2 return (
3    <div className="App">
4    <OverlayTrigger trigger="click" placement="right" overlay={popover}>
5      <Button variant="success">Click to trigger popover</Button>
6    </OverlayTrigger>
7    </div>
8  );

Finally, your App.js rendering the bootstrap popover should look like this . You can find the same code from react-bootstrap's official documentation here.

1import React from 'react';
2import 'bootstrap/dist/css/bootstrap.min.css';
3import Popover from 'react-bootstrap/Popover';
4import OverlayTrigger from 'react-bootstrap/OverlayTrigger';
5import Button from 'react-bootstrap/Button';
8function App() {
9  const popover = (
10    <Popover id="popover-basic">
11      <Popover.Title as="h3">Popover title</Popover.Title>
12      <Popover.Content>
13        Popover content <strong>some strong content</strong> Normal content again
14      </Popover.Content>
15    </Popover>
16  );
19  return (
20    <div className="App">
21    <OverlayTrigger trigger="click" placement="right" overlay={popover}>
22      <Button variant="success">Click to trigger popover</Button>
23    </OverlayTrigger>
24    </div>
25  );
28export default App;

To see the popover inside the root directory, run the following:

1npm start

This will spin up a local development server (usually on port 3000) and you can see the popover button and the popover itself when you click the button. Clicking the same button again dismisses the popover.

Dismiss Popover on Outside Click

Let's look at the two simplest ways of dismissing the popover.

Use rootClose Prop

You can pass the additional prop rootClose to your OverlayTrigger component to dismiss the popover when on an outside click.

2    <OverlayTrigger trigger="click" rootClose placement="right" overlay={popover}>

In the absence of this prop, a false Boolean value is taken by default by the parent component. When you pass this prop, the value is set to true and the onHide method is fired when the user clicks outside the overlay. The onHide method is just a callback invoked by the overlay when it wishes to be hidden and is required if rootClose is specified.

Use Focus Trigger to Dismiss Popover

Another method to dismiss the popover is by using a different trigger event. You can use the hover or focus trigger instead of the click trigger to achieve this. The hover trigger isn't the correct solution since it removes the clicking action on the popover, rendering it a tooltip. Use the focus trigger instead.

2<OverlayTrigger trigger="focus" placement="right" overlay={popover}>

This will close the popover when the user clicks outside of it.

If you get any depreciated warnings or errors, you can use the exact version of react-bootstrap as this guide by updating your package.json file and running the command npm i .

2"bootstrap": "^4.4.1",
3"react-bootstrap": "^1.0.1",


Using attractive UI elements enhances the look of your app and makes the design more appealing. However, while using any UI element you must make sure that you're giving some flexibility to the end-user. A layman expects popovers to work like any popup and hence any similar functionalities should be implemented if they're not implemented by the default library. Using the rootClose prop is the safest, cleanest, and most accurate method, whereas using a different trigger than click could lead to inhomogeneity in your app. Both hover and focus triggers aren't handled well by devices, such as mobiles and tablets. Use the second method when your app is not catering to mobile-like devices.