Last active
February 15, 2020 06:03
-
-
Save gion/6e202e6ef2740c8c1a3e883c82ed7c85 to your computer and use it in GitHub Desktop.
This file contains 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
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