react-handle-outside-click
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

react-handle-outside-click

A react hook to handle clicks outside an element.

Usage

  1. Attach a ref to the element outside which you want to watch clicks.
  2. Call the hook. Pass the ref as the first argument and the handler as the second.
  3. ???
  4. PROFIT

If the handler is a simple () => {} and if needed wrap it a useCallback

JavaScript

import { useOutsideClick } from 'react-handle-outside-click';

const App = () => {
  const ref = React.useRef()

  const handleOutsideClick = () => {
    console.log("hello")
  }

  useOutsideClick(ref, handleOutsideClick);

  return (
    <div ref={ref}>
      Hello World
    </div>
  );
};

TypeScript

import { useOutsideClick } from 'react-handle-outside-click';

const App = () => {
  const ref = React.useRef<HTMLDivElement>(null)

  const handleOutsideClick = () => {
    console.log("hello")
  }

  useOutsideClick(ref, handleOutsideClick);

  return (
    <div ref={ref}>
      Hello World
    </div>
  );
};

Package Sidebar

Install

npm i react-handle-outside-click

Weekly Downloads

4

Version

1.0.0

License

MIT

Unpacked Size

9.99 kB

Total Files

12

Last publish

Collaborators

  • architbhonsle