Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] useOnClickOutside doesn't allow null ref #663

Open
OskarD opened this issue Dec 16, 2024 · 4 comments · May be fixed by #680
Open

[BUG] useOnClickOutside doesn't allow null ref #663

OskarD opened this issue Dec 16, 2024 · 4 comments · May be fixed by #680
Labels
bug Something isn't working

Comments

@OskarD
Copy link

OskarD commented Dec 16, 2024

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

@OskarD OskarD added the bug Something isn't working label Dec 16, 2024
@Xevion
Copy link

Xevion commented Dec 18, 2024

Yeah, just hit this while updating dependencies. It started erroring before I updated from 2.9.1 to 3.1.0, though; so I don't think this is a mere regression.

Regardless, what's the intended fix here? Kinda frustrated.

@arsinclair
Copy link

arsinclair commented Dec 23, 2024

Before a fix is available you can override the types like so:

useOnClickOutside<HTMLUListElement>(ref as React.RefObject<HTMLUListElement>, () => setIsOpen(false));

although, I am not endorsing it.

@macutko
Copy link

macutko commented Feb 10, 2025

Im hitting the same error with useResizeObserver . Is this potentially related? I hit this error when updating to react v19.

@elee1766 elee1766 linked a pull request Feb 26, 2025 that will close this issue
@rik-iso
Copy link

rik-iso commented Mar 8, 2025

useHover also affected. I've added a usehooks.d.ts:

import 'usehooks-ts'

  // React 19 issue: https://github.com/juliencrn/usehooks-ts/issues/663
declare module 'usehooks-ts' {
  declare function useOnClickOutside<T extends HTMLElement = HTMLElement>(
    ref: RefObject<T | null | undefined> | RefObject<T | null | undefined>[],
    handler: (event: MouseEvent | TouchEvent | FocusEvent) => void,
    eventType?: EventType,
    eventListenerOptions?: AddEventListenerOptions
  ): void

  declare function useHover<T extends HTMLElement = HTMLElement>(elementRef: RefObject<T | null | undefined>): boolean;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
5 participants