Package Exports
- react-event-listener
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-event-listener) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
React event listener
A React component for binding events on the global scope.
Getting Started
npm install react-event-listener
Usage
import React, {Component} from 'react';
import EventListener from 'react-event-listener';
class MyComponent extends Component {
handleResize = () => {
console.log('resize');
};
handleMouseMove = () => {
console.log('mousemove');
};
render() {
return (
<div>
<EventListener target={window} onResize={this.handleResize} />
<EventListener target={document} onMouseMove={this.handleMouseMove} capture={true} />
</div>
);
}
}
Note on Performance
You should avoid passing inline functions for listeners, because this creates a new Function
instance on every
render, defeating EventListener
's shouldComponentUpdate
, and triggering an update cycle where it removes its old
listeners and adds its new listeners (so that it can stay up-to-date with the props you passed in).
Note on Testing
In this issue from React, TestUtils.Simulate.
methods won't bubble up to window
or document
. As a result, you must use document.dispatchEvent
or simulate event using native DOM api.
See our test cases for more information.
Contributing
Note: you need to have Flow 0.23.0 or greater to be installed.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Collaborators
- Andy Edwards (jedwards1211)
License
MIT