UNPKG

echarts-for-react-fc

Version:

Using ECharts in React, encapsulating it within a functional component, and providing a hook for consumers to consume.

42 lines (41 loc) 993 B
import { useRef as i, useCallback as r } from "react"; import { isObject as d } from "lodash-es"; const y = () => { const o = i(null), n = i([]), s = r(() => { var t; return (t = o.current) == null ? void 0 : t.getEChartsInstance(); }, []), h = r(() => { var t; return (t = o.current) == null ? void 0 : t.getEChartsReady(); }, []), c = r(() => { n.current.length > 0 && (n.current.forEach( (t) => { var e; return (e = s()) == null ? void 0 : e.setOption(t.option, t.opts); } ), n.current = []); }, [s]), f = r( (t, e, l) => { var u; let a = {}; d(e) ? a = e : a = { notMerge: e, lazyUpdate: l }, h() ? (c(), (u = s()) == null || u.setOption(t, a)) : n.current.push({ option: t, opts: a }); }, [h, s, c] ), p = r( (t) => { t && c(); }, [c] ); return { chartRef: o, setChartOption: f, handleListenChartReady: p }; }; export { y as default };