Usage Event Emitter package in react app
import EventEmitter from 'eventemitter3';
const eventEmitter = new EventEmitter();
const Emitter = {
on: (event, fn) => eventEmitter.on(event, fn),
once: (event, fn) => eventEmitter.once(event, fn),
off: (event, fn) => eventEmitter.off(event, fn),
emit: (event, payload) => eventEmitter.emit(event, payload)
}
Object.freeze(Emitter);
export default Emitter;
componentDidMount() {
// listens all the time
Emitter.on('event_name', () => callback());
// listens only to the first event
Emitter.once('event_name', () => callback());
}
componentWillUnmount() {
Emitter.off('event_name');
}
$ npm install
$ npm start
$ npm build