React hooks and components for construction of simple event emitters
npm install @byteclaw/use-event-emitterReact hooks and components for construction of simple event emitters.
``console`
npm install @byteclaw/use-event-emitter
yarn add @byteclaw/use-event-emitter
Here is a quick example how to use these hooks.
`jsx
import {
EventEmitterContext,
useEventEmitterInstance,
useEventEmitter,
} from '@byteclaw/use-event-emitter';
import React, { useEffect } from 'react';
function Comp() {
// returns the event emitter from the context
const eventEmitter = useEventEmitter();
useEffect(() => {
const unsubscribe = eventEmitter.on('test', (...args) => console.log(args));
return () => unsubscribe();
}, []);
}
function App({ children }) {
// returns the new instance of event emitter
const eventEmitter = useEventEmitterInstance();
return (
);
}
``