1.0.6 • Published 6 years ago

cross-event v1.0.6

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

CROSS-EVENT

Platform agnostic EventEmitter.

Installation

npm install --save cross-event

or

yarn add cross-event

Usage Example

import CrossEvent from 'cross-event';

/*
 * RECEIVER COMPONENT
 */
class Receiver extends PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      data: 'no data',
    };

    this.handler = this.handler.bind(this);
  }

  handler(data) {
    this.setState({
      data,
    });
  }

  componentWillMount() {
    this.listener = CrossEvent.on('myCustomEvent', this.handler);
  }

  componentWillUnmount() {
    CrossEvent.rm(this.listener);
  }

  render() {
    return <Text> {this.state.data} </Text>
  }
}

/*
 * SENDER COMPONENT
 */
const Sender = (props) => (
  <TouchableHighlight onPress = {() => CrossEvent.emit('myCustomEvent', 'it works!!!')>
    <Text> Send Event </Text>
  </TouchableHighlight>
);

API

static methoddescription
onreturn value is the id of the event listener or false on error
rmtrue on success otherwise false
rmAlltrue on success otherwise false
emitno return value