You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
622 B

2 years ago
import { useEffect, useRef } from 'react';
export function useEventHandlers(element, eventHandlers) {
const eventHandlersRef = useRef();
useEffect(function addEventHandlers() {
if (eventHandlers != null) {
element.instance.on(eventHandlers);
}
eventHandlersRef.current = eventHandlers;
return function removeEventHandlers() {
if (eventHandlersRef.current != null) {
element.instance.off(eventHandlersRef.current);
}
eventHandlersRef.current = null;
};
}, [
element,
eventHandlers
]);
}