Created
June 26, 2015 10:31
-
-
Save glenjamin/e333da671d563f2321ce to your computer and use it in GitHub Desktop.
React Capturing all links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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