diff --git a/src/Chart.tsx b/src/Chart.tsx index cc9e977..428b252 100644 --- a/src/Chart.tsx +++ b/src/Chart.tsx @@ -30,21 +30,23 @@ export const Chart = forwardRef((props, ref) => { const [init, setInit] = useState(false); useEffect(() => { - if (containerRef.current) { - chartRef.current = new G2Chart({ - container: containerRef.current, - renderer, - }); - setInit(true); - } + if (chartRef.current || !containerRef.current) return; + chartRef.current = new G2Chart({ + container: containerRef.current, + renderer, + }); + setInit(true); return () => { - if (chartRef.current) chartRef.current.destroy(); + if (chartRef.current) { + chartRef.current.destroy(); + chartRef.current = undefined; + } }; - }, []); + }, [renderer]); useEffect(() => { if (init) onInit?.(); - }, [init]); + }, [init, onInit]); useEffect(() => { if (chartRef.current && options) { diff --git a/test/demos/render.tsx b/test/demos/render.tsx index 12261c3..4e6fd0d 100644 --- a/test/demos/render.tsx +++ b/test/demos/render.tsx @@ -1,11 +1,15 @@ -import React from "react"; +import React, { StrictMode } from "react"; import { createRoot } from "react-dom/client"; export function render(Demo) { return () => { const container = document.createElement("div"); const root = createRoot(container); - root.render(); + root.render( + + + + ); return container; }; }