Skip to content

Instantly share code, notes, and snippets.

@classiemilio
Last active March 3, 2018 01:00
Show Gist options
  • Save classiemilio/36e60dff1e1c37d04d86a1f251b5b66d to your computer and use it in GitHub Desktop.
Save classiemilio/36e60dff1e1c37d04d86a1f251b5b66d to your computer and use it in GitHub Desktop.
const React = require(‘react’);
const ReactDOM = require(‘react-dom’);
const MyTooltipComponent = require('my-tooltip-component');
class MyGraphComponent extends React.Component {
componentDidMount() {
this.drawChart();
}
shouldComponentUpdate() {
return false;
}
componentWillUnmount() {
ReactDOM.unmountComponentAtNode(this.tooltipTarget);
}
drawChart() {
/*
D3 code to create our visualization by appending onto this.svg
*/
// At some point we render a child, say a tooltip
const tooltipData = ...
this.renderTooltip([50, 100], tooltipData);
}
render() {
return (
<div>
<div ref={(elem) => { this.tooltipTarget = elem; }} />
<svg ref={(elem) => { this.svg = elem; }}>
</svg>
</div>
);
}
renderTooltip(coordinates, tooltipData) {
const tooltipComponent = (
<MyTooltipComponent
coordinates={coordinates}
data={tooltipData} />
);
ReactDOM.render(tooltipComponent, this.tooltipTarget);
}
}
@klairetan
Copy link

Cool example! I might be missing something, but I was wondering how this would allow the chart to update in response to changes in its data?

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