Skip to content

Instantly share code, notes, and snippets.

@gion
Last active February 15, 2020 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gion/6e202e6ef2740c8c1a3e883c82ed7c85 to your computer and use it in GitHub Desktop.
Save gion/6e202e6ef2740c8c1a3e883c82ed7c85 to your computer and use it in GitHub Desktop.
import React, { useRef, useEffect } from 'react';
export default ({
artifacts,
width,
height,
}) => {
const root = useRef();
const context = useRef();
useEffect(() => {
if (!context.current) {
// initial mount
// create d3 context here only when the input data is available
// Render stuff inside the div you created
// finally store it
if (artifacts.length) {
context.current = {
chartApi: new D3BubbleChart(root.current, {
width,
height,
artifacts
})
};
}
} else {
// pass updates onto the d3 component
context.current.chartApi.update({
width,
height,
artifacts
});
}
}, [artifacts, width, height]);
return <div ref={root} />;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment