How to use ClickAwayListener Component in ReactJS ? Last Updated : 01 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Detect if a click event happened outside an element. Whenever the user clicks somewhere in the document, this listener works. Material UI for React has this component available for us, and it is very easy to integrate.Prerequisites:React JSNPM & Node.jsMaterial UIApproach:To use ClickAwayListener Component in ReactJS we will wrap the clickAwayListener component around the React Code where we want to detect the click. We will create a button inside and detect the click outside this button component.Steps to Create React Application And Installing Modules:Step 1: Create a React application using the following command.npx create-react-app foldernameStep 2: After creating your project folder i.e. foldername, move to it using the following command.cd foldernameStep 3: After creating the ReactJS application, Install the material-ui modules using the following command.npm i @material-ui/coreProject Structure:Project StructureThe updated dependencies in package.json file will look like."dependencies": { "@material-ui/core": "^4.12.4", "@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", "web-vitals": "^2.1.4}Example: This example implements clickAwayListener to capture the click outside the button. JavaScript // Filename - App.js import React from "react"; import { lightBlue } from "@material-ui/core/colors"; import ClickAwayListener from "@material-ui/core/ClickAwayListener"; export default function App() { const [open, setOpen] = React.useState(false); const handleClickAwayEvent = () => { setOpen(false); }; const handleClickEvent = () => { setOpen((prev) => !prev); }; return ( <div style={{ display: "block", padding: 30 }}> <h4> How to use ClickAwayListner Component in ReactJS? </h4> <ClickAwayListener onClickAway={handleClickAwayEvent} > <div style={{ position: "relative", }} > <button type="button" onClick={handleClickEvent} > ClickAwayListener Demo </button> {open ? ( <div style={{ width: 150, position: "absolute", right: 0, fontSize: 9, border: "1px solid", left: 0, backgroundColor: lightBlue, zIndex: 1, padding: 3, top: 28, }} > It will be visible until you click outside this box </div> ) : null} </div> </ClickAwayListener> </div> ); } Step to Run Application: Run the application using the following command from the root directory of the project.npm startOutput: Now open your browser and go to http://localhost:3000/, you will see the following output. Comment More infoAdvertise with us Next Article How to use ClickAwayListener Component in ReactJS ? gouravhammad Follow Improve Article Tags : JavaScript Web Technologies ReactJS Material-UI React-Questions +1 More Similar Reads How To Use Modal Component In ReactJS? Modals are a popular UI component used to display content in a pop-up window like alerts, forms, or additional information. In ReactJS, there are multiple ways to implement a modal. In this article, we will discuss two common approaches: using reusable functional components and using the Material-UI 4 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 How to use SnackBar Component in ReactJS ? ReactJS provides developers with a wide range of components to create interactive and dynamic web applications. One such component is the SnackBar, which is commonly used to display temporary messages or notifications to users. Creating a SnackBar component allows for the presentation of these messa 2 min read How to use Popover Component in ReactJS ? A Popover is a graphical control which is a container-type element that hovers over the parent window, and it makes sure that all other interaction is blocked until it is selected. Material UI for React has this component available for us, and it is very easy to integrate.PrerequisitesA basic unders 2 min read How to use SpeedDial Component in ReactJS ? SpeedDial is a quick action button for the user from where the user can click over it and easily access the various option provided in the dial. Material UI for React has this component available for us, and it is very easy to integrate. We can use the following approach in ReactJS to use SpeedDial 2 min read How to use Link Component in React JS? Material-UI provides a set of components that can be seamlessly integrated with React to create visually appealing and functional navigation elements. The Link component allows you to easily customize anchor elements with our own theme colors and typography styles. Material UI for React has this com 2 min read How to use Fade Component in React JS ? The Fade Component, available in Material UI for React, seamlessly adds a fade animation to a child element or component. Integrating this component into a React JS project is straightforward and enhances the user interface with ease.Prerequisites:React JSMaterial UIPopper Transitions: The Transitio 2 min read How to detect click outside React component ? We can use the createRef() Â method to create a reference for any element in the class-based component. Then we can check whether click event occurred in the component or outside the component. In the functional component, we can use the useRef() hook to create a reference for any element. Creating R 2 min read How to set Parent State from Children Component in ReactJS? To set the parent state from a child component, we use Reactâs unidirectional data flow. Instead of directly passing data, we pass a function that enables the child to send data back to set the parent state.Prerequisites:React JSuseState HookApproachTo set parent state from child component in React, 2 min read How to create components in ReactJS ? Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C 3 min read Like