Skip to content

Instantly share code, notes, and snippets.

@mstaicu
Last active July 15, 2019 07:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstaicu/4ffe41b6980ad35d82deae4e32582ef0 to your computer and use it in GitHub Desktop.
Save mstaicu/4ffe41b6980ad35d82deae4e32582ef0 to your computer and use it in GitHub Desktop.
A React Higher-order Component for sending analytics based on the original post of David Tang for DailyJS
import React from 'react';
export default function(mapPropsToData, WrappedComponent) {
return function(props) {
function onClick(event) {
if (event.target.tagName === 'A') {
const data = mapPropsToData ? mapPropsToData(props) : {};
// Process data
}
}
return (
<div onClick={onClick}>
<WrappedComponent {...props} />
</div>
);
};
}
import React, { Component } from 'react';
import AnalyticsHOC from './AnalyticsHOC';
class ComponentToWrap extends Component {
render() {
//...
}
}
export default AnalyticsHOC(function(props) {
return {
title: props.title,
};
}, ComponentToWrap);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment