0.1.0 • Published 9 years ago

react-global-event-method v0.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

Allows declarative event listeners for global events on react components. Inspired by this comment

Install

npm install --save react-global-event-method

Usage

The arguments are onGlobalEvent(eventName, eventTarget=window, useCapture=false). Cleanup is done for you, and you may register multiple handlers and multiple events per handler.

import onGlobalEvent from 'react-global-event-method';

class MyComponent extends React.Component {
  @onGlobalEvent('click')
  handleGlobalClick(event){
    this.setState(...);
  }

  render(){
    // ...
  }
}

NOTE!

If you declare componentDidMount or componentWillUnmount they must be above any @onGlobalEvent listeners.