UNPKG

@preact-hooks/event

Version:

A custom Preact Hook that provides a declarative useEventListener.

2 lines (1 loc) 295 B
import{useRef as r,useEffect as e}from"preact/hooks";export default function(t,n,o){void 0===o&&(o=window);var i=r(null);e(()=>{i.current=n},[n,i.current]),e(()=>{if(o&&o.addEventListener){var r=r=>i.current(r);return o.addEventListener(t,r),()=>{o.removeEventListener(t,r)}}},[t,o,i.current])}