I need to set some conditional properties of a Highcharts object and then render it. This is my trial and error code and results so far.
const Charts = ReactHighcharts.withHighcharts(Highstock);
public render(): React.ReactNode {
const chart = new Charts();
// set some passed in options
const chartOptions: Options | undefined = this.props.opts;
chart.config = chartOptions;
if(this.drillable()) {
chartOptions.chart = {};
chartOptions.chart.events = {
drilldown: (event: Highcharts.ChartDrilldownEvent) => {
if (!event.seriesOptions) {
if(event.point !== undefined) {
// @ts-ignore
const pathRoot: List<number> = event.point.path;
const drilldownSeries = this.getDrillSeries(pathRoot);
chart.addSeriesAsDrilldown(event.point, drilldownSeries);
}
}
}
};
}
// maybe add some other conditional configurations
return (
<div>
{ chart }
</div>
);
}
This results in "Objects are not valid as a React child. If you meant to render a collection of children, use an array instead"
Wrapping it in an array like this and trying to render the 'elements' gives the same error
const elements: any[] = [];
elements.push(chart);
Also tried some variations of creating JSX elements like this
const elements: JSX.Element[] = [];
elements.push(React.createElement(chart));
which results in: "Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. "
Also tried just rendering just the element, as in
{ React.createElement(chart) }
which gives the same error again.
Edit:
I think I'm getting there but still missing something. i.e.
<ReactHighcharts
config={chartOptions}
callback={this.chartCallback}
/>
I set up my 'chartOptions', and then in chartCallback, I need to mutate those same chartOptions again to add the events(chartOptions.chart.events = {drilldown: etc}).
I tried pulling the chartOptions from the chart object inside the callback but this does not mutate the config. Is there a way to get access to the config inside the callback ? Thanks again.