Skip to content

Instantly share code, notes, and snippets.

@glenjamin
Created June 26, 2015 10:31
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 glenjamin/e333da671d563f2321ce to your computer and use it in GitHub Desktop.
Save glenjamin/e333da671d563f2321ce to your computer and use it in GitHub Desktop.
React Capturing all links
// Helper function
function findAnchor(node) {
while (node.nodeName.toLowerCase() != 'a') {
if (!node.parentNode) return false;
node = node.parentNode;
}
return node;
}
// Capture component
var LinkCapture = React.createClass({
captureLinks(event) {
var anchor = findAnchor(event.target);
if (anchor && anchor.href) {
event.preventDefault();
alert("Do something with this URL " + anchor.href);
}
},
render: function() {
return (
<div onClick={this.captureLinks}>
{this.props.children}
</div>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment