{"id":14276,"url":"\/distributions\/14276\/click?bit=1&hash=721b78297d313f451e61a17537482715c74771bae8c8ce438ed30c5ac3bb4196","title":"\u0418\u043d\u0432\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043b\u044e\u0431\u043e\u0439 \u0442\u043e\u0432\u0430\u0440 \u0438\u043b\u0438 \u0443\u0441\u043b\u0443\u0433\u0443 \u0431\u0435\u0437 \u0431\u0438\u0440\u0436\u0438","buttonText":"","imageUuid":""}

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.

0
Комментарии
-3 комментариев
Раскрывать всегда