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 · 2 comments
Open

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

OskarD opened this issue Dec 16, 2024 · 2 comments
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.

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
Development

No branches or pull requests

3 participants