React Popover - Show and Hide the Popover

User Interaction

To specify when the Popover should be shown and hidden, set the showEvent and hideEvent properties. These properties can accept several events at once as well as an object.

  • import React from 'react';
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Popover } from 'devextreme-react/popover';
  •  
  • const renderContent = () => {
  • return (
  • <p>Popover content</p>
  • );
  • };
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <div>
  • <img id="image" src="https://url/to/an/image" />
  • <Popover
  • target="#image"
  • showEvent="dxhoverstart"
  • hideEvent="dxhoverend"
  • contentRender={renderContent}
  • />
  • </div>
  • );
  • }
  • }
  •  
  • export default App;

View Demo

The Popover can also be hidden when a user clicks outside it. To control this behavior of the Popover, use the closeOnOutsideClick property.

  • import React from 'react';
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Popover } from 'devextreme-react/popover';
  •  
  • const renderContent = () => {
  • return (
  • <p>Popover content</p>
  • );
  • };
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <div>
  • <img id="image" src="https://url/to/an/image" />
  • <Popover
  • target="#image"
  • showEvent="dxhoverstart"
  • hideEvent="dxhoverend"
  • contentRender={renderContent}
  • closeOnOutsideClick={false}
  • />
  • </div>
  • );
  • }
  • }
  •  
  • export default App;

API

NOTE
In this article, the Button UI component is used to demonstrate how to show and hide the Popover. This choice is made for purely demonstrational purposes, and you can do the same operations using another UI component following the same guidelines.

To show or hide the Popover programmatically, call the show() or hide() method. The same thing can be done using the toggle(showing) method. Pass true or false to this method to show or hide the Popover, respectively. This approach is more typical of jQuery and ASP.NET MVC Controls.

The show() method called without arguments shows the Popover for the target specified beforehand. If you need to change the target once, call the show(target) method.

With Angular, Vue, or React, use a different technique. Bind the visible property of the Popover UI component to a component property. After that, change them, and the Popover will appear or disappear.

  • import React from 'react';
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Popover } from 'devextreme-react/popover';
  • import { Button } from 'devextreme-react/button';
  •  
  • const renderContent = () => {
  • return (
  • <p>Popover content</p>
  • );
  • }
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  •  
  • this.state = {
  • isPopoverVisible: false
  • };
  •  
  • this.showPopover = this.showPopover.bind(this);
  • this.hidePopover = this.hidePopover.bind(this);
  • }
  •  
  • showPopover() {
  • this.setState({
  • isPopoverVisible: true
  • });
  • }
  •  
  • hidePopover() {
  • this.setState({
  • isPopoverVisible: false
  • });
  • }
  •  
  • render() {
  • return (
  • <div>
  • <img id="image" src="https://url/to/an/image" />
  • <Popover
  • target="#image"
  • visible={this.state.isPopoverVisible}
  • contentRender={renderContent}
  • onHiding={this.hidePopover}
  • />
  • <Button
  • text="Show the Popover"
  • onClick={this.showPopover}
  • />
  • <Button
  • text="Hide the Popover"
  • onClick={this.hidePopover}
  • />
  • </div>
  • );
  • }
  • }
  •  
  • export default App;

Events

To execute certain commands before or after the Popover was shown/hidden, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the UI component, assign it to the corresponding onEventName property when you configure the UI component.

  • import React from 'react';
  • import 'devextreme/dist/css/dx.common.css';
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Popover } from 'devextreme-react/popover';
  •  
  • class App extends React.Component {
  • onShowing(e) {
  • // Handler of the 'showing' event
  • }
  •  
  • onShown(e) {
  • // Handler of the 'shown' event
  • }
  •  
  • onHiding(e) {
  • // Handler of the 'hiding' event
  • }
  •  
  • onHidden(e) {
  • // Handler of the 'hidden' event
  • }
  •  
  • render() {
  • return (
  • <Popover ...
  • onShowing={this.onShowing}
  • onShown={this.onShown}
  • onHiding={this.onHiding}
  • onHidden={this.onHidden}
  • />
  • );
  • }
  • }
  •  
  • export default App;

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
  • const hiddenEventHandler1 = function (e) {
  • // First handler of the "hidden" event
  • };
  •  
  • const hiddenEventHandler2 = function (e) {
  • // Second handler of the "hidden" event
  • };
  •  
  • $("#popoverContainer").dxPopover("instance")
  • .on("hidden", hiddenEventHandler1)
  • .on("hidden", hiddenEventHandler2);
See Also