@magicbell/react-headless
Version:
Hooks to build a notification inbox
28 lines • 1.02 kB
JavaScript
import { useEffect } from 'react';
import { eventAggregator } from '../lib/realtime.js';
/**
* React hook to listen to events.
*
* @param event Name of the event
* @param handler Callback function
* @param options
*/
export default function useMagicBellEvent(event, handler, options = { source: 'any' }) {
useEffect(() => {
const callback = (args = {}) => {
if (options.source === 'remote' && args.source !== 'remote')
return;
if (options.source === 'local' && args.source !== 'local')
return;
handler(args.data, args.source);
};
eventAggregator.on(event, callback);
return () => {
eventAggregator.off(event, callback);
};
// TODO: Update code to follow lint suggestions of add missing dependencies,
// remove [] or wrap parent component in callback
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}
//# sourceMappingURL=useMagicBellEvent.js.map