# reactjs-eventemitter

> Step 1: Install the module as dependency.

Latest version **1.0.6** (published 2020-06-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install reactjs-eventemitter
pnpm add reactjs-eventemitter
yarn add reactjs-eventemitter
bun add reactjs-eventemitter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-06-12 |
| First published | 2020-06-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | arpitansu |

## Links

- npm: https://www.npmjs.com/package/reactjs-eventemitter
- Repository: https://github.com/arpitansu/reactjs-eventemitter
- Homepage: https://github.com/arpitansu/reactjs-eventemitter.git
- Issues: https://github.com/arpitansu/reactjs-eventemitter/issues
- npm.io page: https://npm.io/package/reactjs-eventemitter

## Recent versions

- 1.0.6 (latest) — 2020-06-12
- 1.0.5 — 2020-06-10
- 1.0.4 — 2020-06-10
- 1.0.3 — 2020-06-10
- 1.0.2 — 2020-06-10
- 1.0.1 — 2020-06-10
- 1.0.0 — 2020-06-10

## README

Step 1: Install the module as dependency.

```
npm i reactjs-eventemitter 
```

Step 2: Require the module.

```
import EventEmitter from "reactjs-eventemitter";
or
const EventEmitter = require("reactjs-eventemitter")
```

Step 3: Use the module.

```
// your app class

import React from 'react';

import Parent from "./parent";
import Child from "./child";

// here both parent and child are independent of each other

function App() {
  return (
    <div className="App">
        <Parent /> // parent or sibling component
        <Child /> // child or sibling component
    </div>
  );
}

export default App;
```


```
// parent class or sibling class

import React from 'react'
import EventEmitter from "reactjs-eventemitter";

export default function parent() {

    EventEmitter.subscribe('buttonClick', event => {

        console.log("button pressed inside child");
        console.log(event)

    })

    return (
        <div>
            This is parent component 
        </div>
    )

}
```

>[!Warning] When using useEffect hook use subscribe inside useEffect otherwise it'll be called twice
```
import React, { useEffect } from 'react'
const EventEmitter = require("reactjs-eventemitter")




export default function Parent() {

    useEffect(() => {
        console.log("parent component loading...")


        EventEmitter.subscribe('buttonClick', event => {

            console.log(event)
        
        })
        
        EventEmitter.subscribe('buttonClick2', event => {
        
            console.log(event)
        
        })
        
        
        EventEmitter.subscribe('buttonClick3', event => {
        
            console.log(event)
        
        })

    })

   
    return (
        <div>
            This is parent componetn 
        </div>
    )

}

```

```
// child class or sibling class

import React from 'react'
import EventEmitter from "reactjs-eventemitter";

export default function child() {


    return (
        <div>
            This is child componetn 

            <button onClick={(event) => EventEmitter.dispatch('buttonClick', event)}>
                Press Me
            </button>
        </div>
    )

}

```

Available Functions.

```
// publish the event (dispatch and emit both are same)

//for redux lovers
EventEmitter.dispatch('eventName', event);
//for angular lovers
EventEmitter.emit('eventName', event)

//subscribe the event
EventEmitter.subscribe('eventName', event => { //logic here })
```

---
_Source: https://npm.io/package/reactjs-eventemitter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
