-
-
Notifications
You must be signed in to change notification settings - Fork 446
Open
Labels
bugSomething isn't workingSomething isn't working
Description
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
vmaerten, Koshkinks, DmytroMyhal, arsinclair, rcmontoy and 33 more
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity
Xevion commentedon 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 commentedon Dec 23, 2024
Before a fix is available you can override the types like so:
although, I am not endorsing it.
macutko commentedon Feb 10, 2025
Im hitting the same error with useResizeObserver . Is this potentially related? I hit this error when updating to react v19.
rik-iso commentedon Mar 8, 2025
useHover
also affected. I've added ausehooks.d.ts
:eersnington commentedon Jun 25, 2025
Bump on this issue. Are there any PRs to fix this?
Vap0r1ze commentedon Jul 10, 2025
This is because this package doesn't support React 19 (at least typings-wise). React 19 made an undocumented change to one of the
useRef
overloads:A PR that fixes these issues would just be one that updates the package to React 19, and then resolves all the type issues
Vap0r1ze commentedon Jul 10, 2025
Nvm there are 2 separate pull requests already that fix this, but maintainer doesn't seem to be around. If you're looking for a replacement,
@mantine/hooks
is pretty good. Even uses animation frames for their useResizeObserver