ayemitter-hook
v1.0.2
Published
React hook component for [ayemitter](https://github.com/lukasbach/ayemitter/tree/master/packages/core).
Downloads
2
Maintainers
Readme
ayemitter-hook
React hook component for ayemitter.
Usage
yarn add ayemitter ayemitter-hook
import { EventEmitter } from 'ayemitter';
import { useEventChangeHandler } from 'ayemitter-hook';
const emitter = new EventEmitter<string>();
const Component = () => {
const [state, setState] = useState('state1');
useEventChangeHandler(
emitter,
() => {
console.log('Hello!');
},
[state]
); // state is a dependency
// The handler is rebinded to the emitter everytime the
// handler or a dependency changes.
return; // ...
};
API
const useEventChangeHandler: <T>(
eventEmitter: EventEmitter<T>,
handler: (payload: T) => void | Promise<void>,
dependencies?: any[]
) => void;