Google News
logo
Popper.js - Interview Questions
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. We can use the Popper Component in ReactJS using the following approach.
 
Creating 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 material-ui modules using the following command.
npm install @material-ui/core

Example :  Now write down the following code in the App.js file. Here, App is our default component where we have written our code. 

import React from 'react';
import Popper from '@material-ui/core/Popper';

export default function App() {
  const [anchorEl, setAnchorEl] = React.useState(null);
  const open = Boolean(anchorEl);
    return (
         <div style={{ display: 'block', padding: 30 }}>
         <h4>How to use Popper Component in ReactJS?</h4>
      <button type="button" onClick={(event) => {
        setAnchorEl(anchorEl ? null : event.currentTarget);
      }}>
        Click Me to Toggle Popper
      </button>
      <Popper
        id={open ? 'simple-popper' : undefined}
        open={open}
        anchorEl={anchorEl}>
        <div style={{
          padding: 2,
          border: '1px solid',
          backgroundColor: 'gray',
        }}>Greetings from Free Time Learn</div>
      </Popper>
    </div>
  );
}

Step to Run Application : Run the application using the following command from the root directory of the project.

npm start

Output : Now open your browser and go to http://localhost:3000/, you will see the following output.
Popper-ReactJS

Advertisement