react-native-unit-components
Version:
Unit React Native components
24 lines (22 loc) • 729 B
JavaScript
import { useEffect, useRef } from 'react';
import { eventBus } from '../utils/eventBus';
/**
* Hook to listen to events from the EventBus
* Automatically subscribes on mount and unsubscribes on unmount
*/
export const useEventListener = listener => {
const actionRef = useRef(listener.action);
// Keep action ref up to date
useEffect(() => {
actionRef.current = listener.action;
}, [listener.action]);
useEffect(() => {
// Subscribe to the event
const unsubscribe = eventBus.on(listener.busEventKey, data => {
actionRef.current(data);
});
// Cleanup: unsubscribe when component unmounts
return unsubscribe;
}, [listener.busEventKey]);
};
//# sourceMappingURL=useEventListener.js.map