use-event-handler
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

use-event-handler

A simple React Hook for listening events of HTML element

Installation

$ yarn add use-event-handler

Usage

export default () => {
  const ref = useRef<HTMLDivElement>(null);
  useEvent('click', event => console.log(event), { element: ref.current });

  return <div ref={ref} />;
};

API

useEvent(type, callback, options)

  • type*: A string of event name
  • listener*: A function that will be called whenever eventName trigger on element.
  • options: An object of { element?: Element, capture?: boolean, passive?: boolean, once?: boolean }, where element is the element to add the event listener, and others will be passed to addEvenetListener. MDN

Package Sidebar

Install

npm i use-event-handler

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

12.3 kB

Total Files

15

Last publish

Collaborators

  • jchang6513