[BUG] useOnClickOutside doesn't allow null ref

Author: OskarDCreated Dec 16, 2024Updated Sep 5, 2025
Labelsbug

Describe the bug

The sample code in https://usehooks-ts.com/react-hook/use-on-click-outside#usage throws a TypeError:

Argument of type 'RefObject<HTMLDivElement | null>' is not assignable to parameter of type 'RefObject<HTMLElement> | RefObject<HTMLElement>[]'.
  Type 'RefObject<HTMLDivElement | null>' is not assignable to type 'RefObject<HTMLElement>'.
    Type 'HTMLDivElement | null' is not assignable to type 'HTMLElement'.
      Type 'null' is not assignable to type 'HTMLElement'.ts(2345)

To Reproduce

  const ref = useRef(null)

  const handleClickOutside = () => {
    // Your custom logic here
    console.log('clicked outside')
  }

  const handleClickInside = () => {
    // Your custom logic here
    console.log('clicked inside')
  }

  useOnClickOutside(ref, handleClickOutside)

Expected behavior

Should accept null

Additional context

No response