refactor: hooks
This commit is contained in:
parent
21588de558
commit
e880c94afa
15
packages/react/src/hooks/use-event.js
Normal file
15
packages/react/src/hooks/use-event.js
Normal 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]);
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ export {default as PropTypes} from 'prop-types';
|
||||||
|
|
||||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||||
export {LatusContext} from '@latus/react/client';
|
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 useLatus} from './hooks/use-latus';
|
||||||
export {default as usePrevious} from './hooks/use-previous';
|
export {default as usePrevious} from './hooks/use-previous';
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user