UNPKG

@ducor/hooks

Version:

A collection of useful React hooks for building modern web applications. Includes hooks for clipboard operations, window events, intervals, timeouts, and more.

18 lines (17 loc) 590 B
import { useEffect } from "react"; import { useCallbackRef } from "../use-outside-click"; /** * `useWindowEvent` is a custom hook that assigns an event listener to `window`. * * @see Docs https://ui.ducor.net/hooks/use-window-event */ const useWindowEvent = (event, handler, options) => { const listener = useCallbackRef(handler); useEffect(() => { window.addEventListener(event, listener, options); return () => { window.removeEventListener(event, listener, options); }; }, [event, listener, options]); }; export default useWindowEvent;