Last active
July 7, 2017 17:51
-
-
Save dvtng/382008a867f9a510f12d5b600dd3cc90 to your computer and use it in GitHub Desktop.
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why would you ever use findDOMNode though ?