useOutsideClick

useOutsideClick is a custom hook that handles a click outside a specified DOM element, like a div.

Import#

import { useOutsideClick } from "@chakra-ui/react"

Return value#

The useOutsideClick hook receives a reference to a DOM element and a handler when a clicks happens outside this given element.

NameTypeDefaultDescription
refobjectThe ref to access the DOM.
handlerfunctionCallback function to handle the click.

This hooks is compatible with mouse and touch events.

Usage#

Editable Example