useOnClickOutside
function useOnClickOutside(fn, externalRef?): RefCallback<HTMLElement>;
Hook that only runs callback on click events outside of the referenced element
Parameters
| Parameter | Type | Description |
|---|---|---|
fn | () => void | the function to be called on click outside |
externalRef? | RefObject<HTMLElement | null> | optional external ref to use instead of the internally managed ref |
Returns
RefCallback<HTMLElement>
a ref to the element where we are listening for clicks
Example
const ref = useRef<HTMLElement | null>(null);
const refCallback = useOnClickOutside(() => close(), ref);