UNPKG

@carbon/ibm-products

Version:
35 lines (33 loc) 1.03 kB
/** * Copyright IBM Corp. 2020, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import { useEffect, useRef } from "react"; //#region src/components/ConditionBuilder/utils/useEvent.ts /** * Copyright IBM Corp. 2016, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const useEvent = (elementOrRef, eventName, callback) => { const savedCallback = useRef(null); useEffect(() => { savedCallback.current = callback; }, [callback]); useEffect(() => { const element = "current" in elementOrRef ? elementOrRef.current : elementOrRef; if (!element) return; const handler = (event) => { if (savedCallback.current) savedCallback.current(event); }; element.addEventListener(eventName, handler); return () => { element.removeEventListener(eventName, handler); }; }, [elementOrRef, eventName]); }; //#endregion export { useEvent };