How to create Popup box in React JS ?
Last Updated :
09 Jan, 2025
Popup boxes, also known as modal or dialog boxes, are common UI elements used to display additional content or interactions on top of the main page. Creating a popup box in React can be achieved using various approaches, including libraries or custom implementations. We will use the Reactjs-popup library to Create Popup Box in React JS.
Prerequisites:
Approach:
To Create Popup Box in React we will install the reactjs-popup library first. Then import the popup component of the library and enclose the popup content inside with the Popup Components along with the required props.
Steps to Create 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 i reactjs-popup
Project Structure:

The updated dependencies in package.json file will lookl like.
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"reactjs-popup": "^2.0.6",
"web-vitals": "^2.1.4"
},
Example 1: This example adds the popup on the homepage of our app using the reactjs-popup library.
JavaScript
// 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.
npm start
Output:
Example 2: This example uses the reactjs-popup library to create a popup modal that appears at the click of a button and closes in the same behavior.
JavaScript
// 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 modal </button>}
modal nested>
{
close => (
<div className='modal'>
<div className='content'>
Welcome to GFG!!!
</div>
<div>
<button onClick=
{() => close()}>
Close modal
</button>
</div>
</div>
)
}
</Popup>
</div>
)
};
Steps to run the application:
npm start
Output:
Similar Reads
How to create Dialog Box in ReactJS? In modern web development, creating interactive and user-friendly interfaces is essential. One common element found in many applications is a dialog box. A dialog box is a component that appears on top of the main content to display information, receive user input, or prompt for confirmation. In thi
2 min read
How to create Dialog Box in ReactJS? Dialogs inform users about a task and can contain critical information, require decisions, or involve multiple tasks. Material UI for React has this component available for us and it is very easy to integrate. We can create the dialog box in ReactJS using the following approach:Creating React Applic
2 min read
How to create a form in React? React uses forms to allow users to interact with the web page. In React, form data is usually handled by the components. When the data is handled by the components, all the data is stored in the component state. You can control changes by adding event handlers in the onChange attribute and that even
5 min read
How to add Popup in NextJS ? In this article, we are going to learn how we can add Popup in NextJs. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. Approach: To add our Popup in NextJs we are going to use the reactjs-popup package. T
3 min read
How to use Popper Component in ReactJS ? A Popper is used to show the part of the content on top of another. It's an alternative feature for react-popper. Material UI for React has this component available for us, and it is simple and very easy to integrate. For perfect positioning, it uses 3rd party library which is Popper.js.Prerequisite
3 min read