humus-old/client/ui/hooks/use-event.js
2019-05-26 08:07:45 -05:00

19 lines
357 B
JavaScript

// 3rd party.
import React, {useEffect} from 'react';
export function useEvent(object, eventName, fn) {
useEffect(() => {
if (!object) {
return;
}
const onEvent = (...args) => {
fn(...args);
};
onEvent();
object.on(eventName, onEvent);
return () => {
object.off(eventName, onEvent);
};
}, [object]);
}