Skip to content

Latest commit

 

History

History
24 lines (17 loc) · 639 Bytes

use-dom-event-listeners.md

File metadata and controls

24 lines (17 loc) · 639 Bytes

useDomEvent

Allows your React component to hook into native DOM events that exist outside the component lifecycle. You can provide an HTML element, window, document or React ref object to serve as the event target.

Examples

const addWindowEvent = useDomEventListeners(window);

addWindowEvent('click', () => {
  console.log('window was clicked!');
}, [/* effect dependencies */]);
const ref = useRef<HTMLDivElement>(null);
const addRefEvent = useDomEventListeners(ref);

addRefEvent('click', () => {
  console.log('ref was clicked!');
}, [/* effect dependencies */]);

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