Skip to content

Instantly share code, notes, and snippets.

@inawrath
Forked from pstoica/OnBlurComponent.jsx
Created April 16, 2018 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inawrath/9f0eac25ef88c911d16c72207cef3b11 to your computer and use it in GitHub Desktop.
Save inawrath/9f0eac25ef88c911d16c72207cef3b11 to your computer and use it in GitHub Desktop.
onBlur for entire react component
var HelloMessage = React.createClass({
render: function() {
return <div tabIndex="1" onBlur={this.onBlur}>
Hello <input type="text" value="wat"/>
</div>;
},
onBlur: function(e) {
var currentTarget = e.currentTarget;
setTimeout(function() {
if (!currentTarget.contains(document.activeElement)) {
console.log('component officially blurred');
}
}, 0);
}
});
React.render(<HelloMessage name="John" />, mountNode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment