How to use use-latest - 1 common examples

To help you get started, we’ve selected a few use-latest examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Andarist / use-onclickoutside / src / index.ts View on Github external
export default function useOnClickOutside(
  ref: React.RefObject,
  handler: Handler | null,
) {
  if (!isBrowser) {
    return
  }

  const handlerRef = useLatest(handler)

  useEffect(() => {
    if (!handler) {
      return
    }

    const listener = (event: PossibleEvent) => {
      if (
        !ref.current ||
        !handlerRef.current ||
        ref.current.contains(event.target as Node)
      ) {
        return
      }

      handlerRef.current(event)

use-latest

A React helper hook for storing latest value in ref object (updated in useEffect's callback).

MIT
Latest version published 2 years ago

Package Health Score

65 / 100
Full package analysis

Popular use-latest functions

Similar packages