Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fullstack-dev3/react-event-emitter
Usage Event Emitter in react app
https://github.com/fullstack-dev3/react-event-emitter
eventemitter3 npm react-scripts reactdom reactjs
Last synced: about 2 months ago
JSON representation
Usage Event Emitter in react app
- Host: GitHub
- URL: https://github.com/fullstack-dev3/react-event-emitter
- Owner: fullstack-dev3
- Created: 2023-07-13T06:12:31.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-13T07:29:50.000Z (over 1 year ago)
- Last Synced: 2023-11-06T22:04:06.339Z (about 1 year ago)
- Topics: eventemitter3, npm, react-scripts, reactdom, reactjs
- Language: JavaScript
- Homepage:
- Size: 320 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# react-event-emitter
Usage [Event Emitter](https://www.npmjs.com/package/eventemitter3) package in react app## Introduction
#### - Define emitter service
```sh
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
```sh
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
```sh
$ npm install
```## Develop
```sh
$ npm start
```## Production
```sh
$ npm build