@byteclaw/use-event-emitter
v1.0.2
Published
React hooks and components for construction of simple event emitters
Downloads
844
Readme
@Byteclaw/use-event-emitter
React hooks and components for construction of simple event emitters.
Installation
npm install @byteclaw/use-event-emitter
yarn add @byteclaw/use-event-emitter
Usage
Here is a quick example how to use these hooks.
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 (
<EventEmitterContext.Provider value={eventEmitter}>
<Comp1 />
</EventEmitterContext.Provider>
);
}