Skip to content

fullstack-dev3/react-event-emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-event-emitter

Usage Event Emitter package in react app

Introduction

- Define emitter service

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;

- Listen the events from emitter service

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');
}

Install

$ npm install

Develop

$ npm start

Production

$ npm build

Releases

No releases published

Packages

No packages published