19 lines
357 B
JavaScript
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]);
|
|
}
|