# react-event-emitter-mixin

> react event emitter mixin

Latest version **0.0.6** (published 2017-07-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-event-emitter-mixin
pnpm add react-event-emitter-mixin
yarn add react-event-emitter-mixin
bun add react-event-emitter-mixin
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2017-07-08 |
| First published | 2015-10-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | mc-zone |
| Maintainers | mc-zone |
| Keywords | react, react-native, event, emitter |

## Links

- npm: https://www.npmjs.com/package/react-event-emitter-mixin
- Repository: https://github.com/mc-zone/react-event-emitter-mixin
- Homepage: https://github.com/mc-zone/react-event-emitter-mixin#readme
- Issues: https://github.com/mc-zone/react-event-emitter-mixin/issues
- npm.io page: https://npm.io/package/react-event-emitter-mixin

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.0.6 (latest) — 2017-07-08
- 0.0.4 — 2016-03-26
- 0.0.3 — 2015-10-08
- 0.0.2 — 2015-10-05
- 0.0.1 — 2015-10-05

## README

# react-event-emitter-mixin [![Build Status](https://travis-ci.org/mc-zone/react-event-emitter-mixin.svg?branch=master)](https://travis-ci.org/mc-zone/react-event-emitter-mixin/) [![npm version](https://badge.fury.io/js/react-event-emitter-mixin.svg)](http://badge.fury.io/js/react-event-emitter-mixin)

Custom event utilities with lifecycle maintenance for React (Both React and React Native)


## Getting Started
Install via npm
```bash
npm i react-event-emitter-mixin --save
```

## Useage
Communicate between components under event emitter.
```javascript
var EventEmitterMixin = require('react-event-emitter-mixin');

var Child = React.createClass({
    mixins:[EventEmitterMixin],
    render(){
        return (
            <View>
                <TouchableOpacity
                    onPress={()=>{
                        this.eventEmitter('emit','eventA','foo','bar');
                    }}
                >
                    <Text>press to emit event</Text>
                </TouchableOpacity>
            </View>
        );
    }
});
var Parent = React.createClass({
    mixins:[EventEmitterMixin],
    componentWillMount(){
        this.eventEmitter('on','eventA',(a,b)=>{
            alert(a); //'foo'
            alert(b); //'bar'
        });
    },
    render(){
        return (
            <View style={{paddingTop:100}}>
                <Child />
            </View>
        );
    }
});

```
Same usage when you are using ReactJs on Web.


## Lifecycle maintenance
Auto clean listeners when components be unmounted.
```javascript
var Child = React.createClass({
    mixins:[EventEmitterMixin],
    componentWillMount(){
        this.eventEmitter('on','eventB',()=>{
            alert('Will not show if `Child` was unmounted or not mounted')
        });
        this.eventEmitter('one','eventB',()=>{
            alert('Once time per lifecycle')
        });
    },
    render(){
        //...
    }
});
var Parent = React.createClass({
    mixins:[EventEmitterMixin],
    name:'parent',
    getInitialState(){
        return {
            mountChild:true,
        }
    },
    render(){
        return (
            <View style={{paddingTop:100}}>
                {this.state.mountChild && (
                    <Child />
                )}
                <TouchableOpacity
                    onPress={()=>{
                        this.setState({mountChild:!this.state.mountChild})
                    }}
                >
                    <Text>mount/unmount child</Text>
                </TouchableOpacity>
                <TouchableOpacity
                    onPress={()=>{
                        this.eventEmitter('emit','eventB');
                    }}
                >
                    <Text>press to emit event</Text>
                </TouchableOpacity>
            </View>
        );
    }
});

```


## API
```javascript
//Add event listener
this.eventEmitter('on', eventName:string, callback:function);

//Add once-only (per lifecycle) event listener
this.eventEmitter('one', eventName:string, callback:function);

//Remove event listeners which come from current component (or remove all)
this.eventEmitter('off', eventName:string, callback:function [,shouldRemoveAll:Boolean] );

//Emit event 
this.eventEmitter('emit', eventName:string [,data1 [,data2,...] ] );
```

## LICENSE
MIT.


## Welcome PR :)

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