refactor: hooks

This commit is contained in:
cha0s 2021-03-23 13:42:52 -05:00
parent 21588de558
commit e880c94afa
2 changed files with 16 additions and 0 deletions

View File

@ -0,0 +1,15 @@
import {useEffect} from 'react';
export default function useEvent(object, eventName, fn) {
useEffect(() => {
if (!object) {
return undefined;
}
const onEvent = (...args) => {
fn(...args);
};
onEvent();
object.on(eventName, onEvent);
return () => object.off(eventName, onEvent);
}, [eventName, fn, object]);
}

View File

@ -5,6 +5,7 @@ export {default as PropTypes} from 'prop-types';
// eslint-disable-next-line import/no-extraneous-dependencies
export {LatusContext} from '@latus/react/client';
export {default as useEvent} from './hooks/use-event';
export {default as useLatus} from './hooks/use-latus';
export {default as usePrevious} from './hooks/use-previous';