Here is modern way to achieve this using React Hooks

import React, { useState } from "react";

const PopUp = ({ idMessage }) => {
  // create state `open` with default as false
  const [open, setOpen] = useState(false);
  return (
    <>
      {/* click of button toggles `open` value therefore visibility */}
      <button
        onClick={() => setOpen(!open)}
        type="button"
        className="btn btn-primary"
        data-toggle="modal"
        data-target={`#${idMessage}`}
      >
        {idMessage}
      </button>
      {/* If open is true show your <div /> */}
      {open && (
        <div
          className="modal fade"
          id={idMessage}
          tabIndex="-1"
          role="dialog"
          aria-labelledby="exampleModalLabel"
          aria-hidden="true"
        >
          content
        </div>
      )}
    </>
  );
};

export default PopUp;
Answer from vitjbr on Stack Overflow
Top answer
1 of 3
5

Here is modern way to achieve this using React Hooks

import React, { useState } from "react";

const PopUp = ({ idMessage }) => {
  // create state `open` with default as false
  const [open, setOpen] = useState(false);
  return (
    <>
      {/* click of button toggles `open` value therefore visibility */}
      <button
        onClick={() => setOpen(!open)}
        type="button"
        className="btn btn-primary"
        data-toggle="modal"
        data-target={`#${idMessage}`}
      >
        {idMessage}
      </button>
      {/* If open is true show your <div /> */}
      {open && (
        <div
          className="modal fade"
          id={idMessage}
          tabIndex="-1"
          role="dialog"
          aria-labelledby="exampleModalLabel"
          aria-hidden="true"
        >
          content
        </div>
      )}
    </>
  );
};

export default PopUp;
2 of 3
3

You could consider creating the popup as a re-usable component, that just renders the props.message. Suppose you have the button in App.js, here is how you can add the click listener on it.

class App extends Component {
  state = {showPopup: false};

  openPopupHandler = () => {
    this.setState({showPopup: true});
  }
  
  closePopupHandler = () => {
    this.setState({showPopup: false});
  }

  
  render() {
    let popup = null;
    if(this.state.showPopup) {
      popup = (<Popup message='the text you need to display' closeMe={this.closePopupHandler}/>);
    } 
    return(
      <div>
      <button onClick={this.clicked}>Click me </button>
      {popup}
      </div>
    );
  }
}

And you can define the popup component as given below.

Popup.js

const popup = (props) => {
return (
  <div>
    <p>{props.message}</p>
    <button onClick={props.closeMe}>Close Popup</button>
  </div>
);
}

Also, style the popup component with the size as per your requirement and with an z-index greater than that of the parent component.

🌐
npm
npmjs.com › package › reactjs-popup
reactjs-popup - npm
This is a simple Demo to demonstrate how you can create Modals, Tooltips, Menus using reactjs-popup.
      » npm install reactjs-popup
    
Published   Sep 06, 2023
Version   2.0.6
Author   Youssouf EL AZIZI
People also ask

Can I create a React popup with TypeScript?
Yes, and TypeScript adds significant value for popup components. Define interfaces for your popup's props; open state, close handler, children, and any content-specific props, to catch type mismatches at compile time rather than runtime. Most major popup libraries, including reactjs-popup, MUI, and Headless UI, ship with TypeScript definitions. The TypeScript React handbook covers component typing patterns in depth.
🌐
popupsmart.com
popupsmart.com › popupsmart conversion rate optimization & digital marketing blog › how to create a react popup & customize it in 4 steps
How to Create a React Popup & Customize it in 4 Steps
How do React popups impact SEO?
Popups themselves don't harm SEO, but intrusive implementations do. Google penalizes mobile interstitials that block page content immediately on load or make content inaccessible without dismissal — this is measured through mobile usability signals and Core Web Vitals. To avoid penalties: delay popup triggers past initial page render, ensure all page content is accessible to Googlebot regardless of popup state, and make dismissal obvious and easy. Exit intent popups that only fire on cursor movement toward the address bar are generally safe, as Google's crawler doesn't trigger mouse events.
🌐
popupsmart.com
popupsmart.com › popupsmart conversion rate optimization & digital marketing blog › how to create a react popup & customize it in 4 steps
How to Create a React Popup & Customize it in 4 Steps
How do I test React popups effectively?
Use Jest and React Testing Library for unit tests, verify the popup opens on trigger click, closes on the close button, and closes on Escape. Add ARIA attribute assertions to catch accessibility regressions. Use Cypress or Playwright for end-to-end keyboard navigation tests. Key edge cases: rapid open/close cycles and correct focus restoration after close. The React Testing Library docs cover accessible selector strategies.
🌐
popupsmart.com
popupsmart.com › popupsmart conversion rate optimization & digital marketing blog › how to create a react popup & customize it in 4 steps
How to Create a React Popup & Customize it in 4 Steps
🌐
PopupSmart
popupsmart.com › popupsmart conversion rate optimization & digital marketing blog › how to create a react popup & customize it in 4 steps
How to Create a React Popup & Customize it in 4 Steps
January 18, 2024 - Trigger Events: Popups activate on specific user actions: button clicks, mouse hovers, scroll depth, or time on page. Using React hooks for event handling ensures smooth responses without performance issues. State Management: React's useState hook manages visibility. The open boolean drives ...
Top answer
1 of 2
6

They called Modal.

You can check the react-modal library.

Here is a simple example of react-modal being used in an app with some custom styles and focusable input elements within the modal content:

import React from 'react';
import ReactDOM from 'react-dom';
import Modal from 'react-modal';

const customStyles = {
  content : {
    top                   : '50%',
    left                  : '50%',
    right                 : 'auto',
    bottom                : 'auto',
    marginRight           : '-50%',
    transform             : 'translate(-50%, -50%)'
  }
};

// Make sure to bind modal to your appElement (http://reactcommunity.org/react-modal/accessibility/)
Modal.setAppElement('#yourAppElement')

class App extends React.Component {
  constructor() {
    super();

    this.state = {
      modalIsOpen: false
    };

    this.openModal = this.openModal.bind(this);
    this.afterOpenModal = this.afterOpenModal.bind(this);
    this.closeModal = this.closeModal.bind(this);
  }

  openModal() {
    this.setState({modalIsOpen: true});
  }

  afterOpenModal() {
    // references are now sync'd and can be accessed.
    this.subtitle.style.color = '#f00';
  }

  closeModal() {
    this.setState({modalIsOpen: false});
  }

  render() {
    return (
      <div>
        <button onClick={this.openModal}>Open Modal</button>
        <Modal
          isOpen={this.state.modalIsOpen}
          onAfterOpen={this.afterOpenModal}
          onRequestClose={this.closeModal}
          style={customStyles}
          contentLabel="Example Modal"
        >

          <h2 ref={subtitle => this.subtitle = subtitle}>Hello</h2>
          <button onClick={this.closeModal}>close</button>
          <div>I am a modal</div>
          <form>
            <input />
            <button>tab navigation</button>
            <button>stays</button>
            <button>inside</button>
            <button>the modal</button>
          </form>
        </Modal>
      </div>
    );
  }
}

ReactDOM.render(<App />, appElement);

You can see more examples here

2 of 2
3

The popup is called a modal. There are multiple libraries that support modals. React bootstrap provides a neat modal. Or if you want to use modal alone there are libraries such as react-modal and react-responsive-modal

🌐
Medium
medium.com › @daniela.sandoval › creating-a-popup-window-using-js-and-react-4c4bd125da57
Creating A PopUp Window Using JS And React | by Daniela Sandoval | Medium
July 22, 2019 - A small guide on creating the infamous “popup” window that is both a blessing and a curse for users with the help of JS and React.
🌐
MakeUseOf
makeuseof.com › home › programming › add pop-up effects to your react.js app
Add Pop-Up Effects to Your React.js App
October 22, 2022 - Finally, you need to add a button to your component that will trigger the pop-up. When you click this button, set the state variable to true, which will cause the pop-up to appear.
Find elsewhere
🌐
CodePen
codepen.io › bastianalbers › pen › PWBYvz
Simple react popup example
class Popup extends React.Component { render() { return ( <div className='popup'> <div className='popup_inner'> <h1>{this.props.text}</h1> <button onClick={this.props.closePopup}>close me</button> </div> </div> ); } } class App extends React.Component { constructor() { super(); this.state = { showPopup: false }; } togglePopup() { this.setState({ showPopup: !this.state.showPopup }); } render() { return ( <div className='app'> <h1>hihi</h1> <button onClick={this.togglePopup.bind(this)}>show popup</button> <button onClick={() => {alert('woooooooot?');}}>try me when popup is open</button> <p>Ganz
🌐
C# Corner
c-sharpcorner.com › article › how-to-create-a-modal-pop-up-in-reactjs-appliaction
How To Create A Modal Pop-Up In ReactJS Application
June 1, 2021 - In this article, we are going to learn how to create a Modal popup in the ReactJS Application. ... Open the newly-created project in Visual Studio code and install the following packages that are needed by using the following command, ... <p>Want to close the pop up?<button type="button" className="link-button" onClick={() => this.isShowModal(true)}> Close</button></p>
🌐
Medium
medium.com › @byron.skoutaris › simple-popup-example-in-react-281a949bc3df
Simple Popup Component In React. In this tutorial we will be building a… | by Byron Skoutaris | Medium
October 14, 2022 - import React, { useState } from "react"; import { Popup } from "./components/Popup/Popup";const App = () => { const [open, setOpen] = useState(false); return ( <div> <button onClick={() => setOpen(true)}> Click to Open Popup</button> {open ? <Popup text="Hello there!" closePopup={() => setOpen(false)} /> : null} </div> ); };export default App; Thank you for reading!
Top answer
1 of 2
3

For class component

In app.js

import React from "react";
import Modal from "./Component/Modal";
import "./styles.css";
class App extends React.Component {
  state = {
    show: false
  };
  showModal = e => {
    this.setState({
      show: !this.state.show
    });
  };
  render() {
    return (
      <div className="App">
        <button
          class="toggle-button"
          id="centered-toggle-button"
          onClick={e => {
            this.showModal(e);
          }}
        >
          {" "}
          show Modal{" "}
        </button>

        <Modal onClose={this.showModal} show={this.state.show}>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis
          deserunt corrupti, ut fugit magni qui quasi nisi amet repellendus non
         
        </Modal>
      </div>
    );
  }
}

export default App;

In component/modal

Make file named index.js

import React from "react";
import "./modal.css";
import PropTypes from "prop-types";

export default class Modal extends React.Component {
  onClose = e => {
    this.props.onClose && this.props.onClose(e);
  };
  render() {
    if (!this.props.show) {
      return null;
    }
    return (
      <div class="modal" id="modal">
        <h2>Modal Window</h2>
        <div class="content">{this.props.children}</div>
        <div class="actions">
          <button class="toggle-button" onClick={this.onClose}>
            close
          </button>
        </div>
      </div>
    );
  }
}
Modal.propTypes = {
  onClose: PropTypes.func.isRequired,
  show: PropTypes.bool.isRequired
};

Make file modal.css

html,
body {
  height: 100%;
}
body {
  background: #eee;
  display: flex;
  justify-content: center;
  align-items: center;
}
.modal {
  width: 500px;
  background: white;
  border: 1px solid #ccc;
  transition: 1.1s ease-out;
  box-shadow: -2rem 2rem 2rem rgba(0, 0, 0, 0.2);
  filter: blur(0);
  transform: scale(1);
  opacity: 1;
  visibility: visible;
}
.modal.off {
  opacity: 0;
  visibility: hidden;
  filter: blur(8px);
  transform: scale(0.33);
  box-shadow: 1rem 0 0 rgba(0, 0, 0, 0.2);
}
@supports (offset-rotation: 0deg) {
  offset-rotation: 0deg;
  offset-path: path("M 250,100 S -300,500 -700,-200");
  .modal.off {
    offset-distance: 100%;
  }
}
@media (prefers-reduced-motion) {
  .modal {
    offset-path: none;
  }
}
.modal h2 {
  border-bottom: 1px solid #ccc;
  padding: 1rem;
  margin: 0;
}
.modal .content {
  padding: 1rem;
}
.modal .actions {
  border-top: 1px solid #ccc;
  background: #eee;
  padding: 0.5rem 1rem;
}
.modal .actions button {
  border: 0;
  background: #78f89f;
  border-radius: 5px;
  padding: 0.5rem 1rem;
  font-size: 0.8rem;
  line-height: 1;
}
#centered-toggle-button {
  position: absolute;
}

This is an example of modal with class component. Please check if this helps you.

@Python

Second example

You can try this, if that does not work. This is bit easy also.

Using react bootstrap module. In App.js

import React from 'react';  
import './App.css';  
import { Button,Modal} from 'react-bootstrap';  
class App extends React.Component {  
  constructor(){  
    super();  
    this.state={  
      show:false  
    }  
  }  
  handleModal(){  
    this.setState({show:!this.state.show})  
  }  
  render(){  
    return (  
      <div>  
        <h2 align='center'>Example of Modal in Reactjs</h2>  
        <div className="modalClass">  
          <Button onClick={()=>this.handleModal()}>Click To Open Modal</Button>  
        </div>  
          
        <Modal show={this.state.show} onHide={()=>this.handleModal()}>  
          <Modal.Header closeButton>This is a Modal Heading</Modal.Header>  
          <Modal.Body>This is a Modal Body</Modal.Body>  
          <Modal.Footer>  
            <Button onClick={()=>this.handleModal()}>Close</Button>  
            <Button onClick={()=>this.handleModal()}>Save</Button>  
          </Modal.Footer>  
        </Modal>  
      </div>  
    )  
  }  
}  
export default App; 

In css file just add

.modalClass {  
  text-align: center;  
  margin-top: 100px;  
} 
2 of 2
1

The first step is to import packages.

import React from "react";
import { Modal, Button, Form } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.css";

Then in App function you can set state to show and hide the modal popup.

function App() {
  const [show, setShow] = useState(false);

  const handleShow = () => setShow(true);

  return (
    <>
      <div
        className="d-flex align-items-center justify-content-center"
        style={{ height: "100vh" }}
      >
        <Button variant="primary" onClick={handleShow}>
          Launch Form modal
        </Button>
      </div>
      <Modal show={show}>
        <Modal.Header closeButton>
          <Modal.Title>Login Form</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <></>
        </Modal.Body>
        <Modal.Footer>
          <Button variant="secondary">Close Modal</Button>
        </Modal.Footer>
      </Modal>
    </>
  );
}

This is just an example, hope this helps you.

🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-create-popup-box-in-reactjs
How to create Popup box in React JS ? - GeeksforGeeks
// Filename - App.js import React from 'react'; import Popup from 'reactjs-popup'; import 'reactjs-popup/dist/index.css'; export default function PopupGfg() { return ( <div> <h4>Popup - GeeksforGeeks</h4> <Popup trigger= {<button> Click to open popup </button>} position="right center"> <div>GeeksforGeeks</div> <button>Click here</button> </Popup> </div> ) }; Steps to run the application: Run the below command in the terminal to run the app.
Published   July 23, 2025
🌐
YouTube
youtube.com › watch
Build a POPUP component in React JS ~ A Beginner Tutorial ...
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Framework7
v5.framework7.io › react › popup
Popup React Component | Framework7 React Documentation
export default class extends React.Component { constructor(props) { super(props); this.state = { popupOpened: false, } } render() { return ( <Page onPageBeforeRemove={this.onPageBeforeRemove.bind(this)}> <Navbar title="Popup"></Navbar> <Block> <p>Popup is a modal window with any HTML content that pops up over App's main content. Popup as all other overlays is part of so called "Temporary Views".</p> <p> <Button fill popupOpen=".demo-popup">Open Popup</Button> </p> <p> <Button fill onClick={() => this.setState({ popupOpened : true })}>Open Via Prop Change</Button> </p> <p> <Button fill onClick=
🌐
Syncfusion
ej2.syncfusion.com › react › documentation › split-button › how-to › open-a-dialog-on-popup-item-click
Open a dialog on popup item click in React Split button component | Syncfusion
import { enableRipple } from '@syncfusion/ej2-base'; import { DialogComponent } from '@syncfusion/ej2-react-popups'; import { ItemModel, MenuEventArgs, SplitButtonComponent } from '@syncfusion/ej2-react-splitbuttons'; import * as React from 'react'; import * as ReactDom from 'react-dom'; enableRipple(true); // To render SplitButton. function App() { let dialogRef: DialogComponent; let items: ItemModel[] = [ { text: 'Help' }, { text: 'About' }, { text: 'Update...' }]; let alertDlgButtons: any [] = [{ buttonModel: { content: 'Ok', isPrimary: true }, 'click': () => { hide(); } }, { buttonModel: {
🌐
Medium
enetoolveda.medium.com › open-a-popup-window-react-db81c8c180e5
Open a PopUp Window, React. Today I will try to show you how to… | by Ernesto Jara Olveda | Medium
February 10, 2020 - Something that we need to clarify first is the fact that by nature browsers don’t allow to open windows, because how we humans like to fill up our sites with Ads and stuff, but it does not matter was we are going to open it no matter what. There is only one rule we must follow. The new window must be open by the users actions. ... $ npm i -S react-router react-router-dom && npm i -D prop-types @types/node @types/react @types/react-router-dom
🌐
npm
npmjs.com › package › react-new-window
react-new-window - npm
Latest version: 1.0.1, last published: 3 years ago. Start using react-new-window in your project by running `npm i react-new-window`. There are 25 other projects in the npm registry using react-new-window.
      » npm install react-new-window
    
Published   Nov 27, 2022
Version   1.0.1
Author   Rubens Mariuzzo