Skip to content

Commit 0d27170

Browse files
authored
Merge pull request #687 from benmusson/feat/chart-ref
Expose ApexCharts Reference
2 parents ed64969 + c7c5fd5 commit 0d27170

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/react-apexcharts.jsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ export default function Charts({
4646
height = "auto",
4747
series,
4848
options,
49+
chartRef,
4950
...restProps
5051
}) {
51-
const chartRef = useRef(null);
52-
let chart = useRef(null);
52+
const chartElementRef = useRef(null);
53+
let chart = chartRef || useRef(null);
5354
const prevOptions = useRef()
5455

5556
useEffect(() => {
5657
prevOptions.current = options;
5758

58-
const current = chartRef.current;
59+
const current = chartElementRef.current;
5960
chart.current = new ApexCharts(current, getConfig());
6061
chart.current.render();
6162

@@ -124,13 +125,14 @@ export default function Charts({
124125

125126
const rest = omit(restProps, Object.keys(Charts.propTypes));
126127

127-
return <div ref={chartRef} {...rest} />;
128+
return <div ref={chartElementRef} {...rest} />;
128129
}
129130

130131
Charts.propTypes = {
131132
type: PropTypes.string.isRequired,
132133
series: PropTypes.array.isRequired,
133134
options: PropTypes.object.isRequired,
134135
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
135-
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
136+
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
137+
chartRef: PropTypes.shape({ current: PropTypes.any })
136138
};

types/react-apexcharts.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference types="react"/>
2-
import { ApexOptions } from "apexcharts";
2+
import ApexCharts, { ApexOptions } from "apexcharts";
33
import React from "react";
44
/**
55
* Basic type definitions from https://apexcharts.com/docs/react-charts/#props
@@ -27,6 +27,7 @@ declare module "react-apexcharts" {
2727
width?: string | number;
2828
height?: string | number;
2929
options?: ApexOptions;
30+
chartRef?: React.RefObject<ApexCharts | null>;
3031
[key: string]: any;
3132
}
3233
export default class ReactApexChart extends React.Component<Props> {}

0 commit comments

Comments
 (0)