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. The reactjs-popup package helps us to integrate different types of popups. So first, we will install the reactjs-popup package and then we will add popups on our homepage.
Create NextJS Application: You can create a new NextJs project using the below command:
npx create-next-app gfg
Install the required package: Now we will install the reactjs-popup package using the below command:
npm i reactjs-popup
Project Structure: It will look like this.
Adding Popup: In this example, we are going to add the popup on the homepage of our app using the package that we installed. For this, add the below content in the index.js file.
index.js
import React from 'react' ; import Popup from 'reactjs-popup' ; import 'reactjs-popup/dist/index.css' ; export default function PopupGfg(){ return ( <div> <h4>NextJs Popup - neveropen</h4> <Popup trigger={<button> Click to open popup </button>} position= "right center" > <div>neveropen</div> <button>Click here</button> </Popup> </div> ) }; |
Explanation: In the above example first, we are importing the Popup component from the react-popup package. After that, we are using our Popup component to add a popup with a button to trigger the popup.
Steps to run the application: Run the below command in the terminal to run the app.
npm run dev