Skip to content

Instantly share code, notes, and snippets.

@dvtng
Last active July 7, 2017 17:51
Show Gist options
  • Save dvtng/382008a867f9a510f12d5b600dd3cc90 to your computer and use it in GitHub Desktop.
Save dvtng/382008a867f9a510f12d5b600dd3cc90 to your computer and use it in GitHub Desktop.
function withLinkAnalytics(mapPropsToData, WrappedComponent) {
class LinkAnalyticsWrapper extends React.Component {
componentDidMount() {
ReactDOM.findDOMNode(this).addEventListener('click', this.onClick);
}
componentWillUnmount() {
ReactDOM.findDOMNode(this).removeEventListener('click', this.onClick);
}
onClick = (e) => {
if (e.target.tagName === 'A') { // Naive check for <a> elements
const data = mapPropsToData ? mapPropsToData(this.props) : {};
sendAnalytics('link clicked', data);
}
};
render() {
// Simply render the WrappedComponent with all props
return <WrappedComponent {...this.props} />;
}
}
return LinkAnalyticsWrapper;
}
@WhoAteDaCake
Copy link

Why would you ever use findDOMNode though ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment