This is a React component that can be used to listen for clicks outside of a given component. As an example, you may need to hide a menu when a user clicks elsewhere on the page.
This component was created specifically to support ES6-style React components. If you want to use a mixin instead, I would recommend the react-onclickoutside mixin.
npm install react-onclickout --save
For React 0.14
or later, use version 2.x
of this package. For React 0.13
or earlier, use version 1.x
of this package.
There are two ways to use this component.
let ClickOutHandler = require('react-onclickout');
class ExampleComponent extends React.Component {
onClickOut(e) {
alert('user clicked outside of the component!');
}
render() {
return (
<ClickOutHandler onClickOut={this.onClickOut}>
<div>Click outside of me!</div>
</ClickOutHandler>
);
}
}
let ClickOutComponent = require('react-onclickout');
class ExampleComponent extends ClickOutComponent {
onClickOut(e) {
alert('user clicked outside of the component!');
}
render() {
return (
<div>Click outside of me!</div>
);
}
}
That's pretty much it. Pull requests are more than welcome!