Skip to content

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

@OskarD

Description

@OskarD

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

Activity

Xevion

Xevion commented on Dec 18, 2024

@Xevion

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

arsinclair commented on Dec 23, 2024

@arsinclair

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

macutko commented on Feb 10, 2025

@macutko

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

linked a pull request that will close this issue on Feb 26, 2025
rik-iso

rik-iso commented on Mar 8, 2025

@rik-iso

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;
}
eersnington

eersnington commented on Jun 25, 2025

@eersnington

Bump on this issue. Are there any PRs to fix this?

Vap0r1ze

Vap0r1ze commented on Jul 10, 2025

@Vap0r1ze

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:

- useRef<T>(initialValue: T | null): RefObject<T>
+ useRef<T>(initialValue: T | null): RefObject<T | null>

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

Vap0r1ze commented on Jul 10, 2025

@Vap0r1ze

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @arsinclair@OskarD@Vap0r1ze@eersnington@macutko

      Issue actions

        [BUG] useOnClickOutside doesn't allow null ref · Issue #663 · juliencrn/usehooks-ts