React Custom Hook: useClickOutside

React Custom Hook: useClickOutside
React Custom Hook: useClickOutside

In this article series, we embark on a journey through the realm of custom React hooks, discovering their immense potential for elevating your development projects. Our focus today is on the "useClickOutside" hook, one of the many carefully crafted hooks available in the collection of React custom hooks.

import useEventListener from "../useEventListener/useEventListener" export default function useClickOutside(ref, cb) { useEventListener("click", e => { if (ref.current == null || ref.current.contains(e.target)) return cb(e) }, document) }

The useClickOutside hook is designed to simplify the process of detecting clicks outside a specified component. By utilizing the useEventListener hook, it listens for click events on the document level, allowing you to trigger a callback function when a click occurs outside the provided component's reference.

One of the main advantages of useClickOutside is its ease of use. Simply import the hook into your component and pass the desired component's reference and a callback function. The hook takes care of the event listener setup and cleanup, saving you time and effort. Plus, it works seamlessly with functional components using the useState and useRef hooks.

The potential applications for useClickOutside are endless. It is particularly useful when implementing modal windows, dropdown menus, or any element that should be closed when a user interacts with anything outside of it. By incorporating useClickOutside, you can enhance the user experience by providing intuitive and efficient interactions.

import { useRef, useState } from "react" import useClickOutside from "./useClickOutside" export default function ClickOutsideComponent() { const [open, setOpen] = useState(false) const modalRef = useRef() useClickOutside(modalRef, () => { if (open) setOpen(false) }) return ( <> <button onClick={() => setOpen(true)}>Open</button> <div ref={modalRef} style={{ display: open ? "block" : "none", backgroundColor: "blue", color: "white", width: "100px", height: "100px", position: "absolute", top: "calc(50% - 50px)", left: "calc(50% - 50px)", }} > <span>Modal</span> </div> </> ) }

To see useClickOutside in action, take a look at the example above. In this case, the ClickOutsideComponent utilizes the hook to toggle the visibility of a modal window. When the user clicks outside the modal, the provided callback function sets the open state to false, closing the modal. This way, the component offers a sleek and user-friendly way to manage the modal's visibility.

Начать дискуссию