useEventListener
Set up an event listener on the target.
API
function useEventListener<T extends EventTarget = EventTarget, E extends Event = Event>(
target: T | React.RefObject<T | null>,
eventName: string,
listener: (event: E) => void,
options?: boolean | { capture?: boolean; once?: boolean; passive?: boolean },
): void;
Parameters:
target: An event target, or a ref object that references an event target (ornull).eventName: Event name.listener: Listener function.options: Similar to the third parameter of theaddEventListenerfunction, but only thecapture,onceandpassiveoptions are used.
Notes
- When
targetis a ref, the listener is attached when the referenced node appears and re-attached when it changes; nothing happens while it isnull. - The
listeneris always the latest render's function, so it never reads stale closures.
Example
The following example counts clicks on a button.
Count: 0
import { useRef, useState } from "react";
import { useEventListener } from "@lilib/hooks";
function Example() {
const buttonRef = useRef<HTMLButtonElement>(null);
const [count, setCount] = useState(0);
useEventListener(buttonRef, "click", () => {
setCount((count) => count + 1);
});
return (
<>
<button ref={buttonRef}>Click</button> Count: {count}
</>
);
}
export default Example;