Highcharts Official React Integration (Legacy)
highcharts-react-official is the previous official React wrapper for the Highcharts charting library, now largely superseded by the newer `@highcharts/react` package. This library (v3.2.3) provides a declarative way to integrate Highcharts, Highstock, and Highmaps into React applications, abstracting away direct DOM manipulation. It supports React versions 16.8.0 and above, and Highcharts core versions 6.0.0 and up. Key features include passing chart options as props, lifecycle management for updates and unmounting, and basic integration with Highcharts modules. While functional and widely used in existing projects, new projects are strongly encouraged to use `@highcharts/react` (v4+), which offers a more 'React-native' API, full ES module support, improved TypeScript definitions, and custom component integration. Releases for `highcharts-react-official` are infrequent as development has shifted to the new package. Its primary differentiator was being the official wrapper, providing a stable, maintained bridge between Highcharts and React before the newer, more opinionated integration was developed. It ships with TypeScript types.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'chart')
cause Highcharts modules were not correctly initialized or the `highcharts` prop was not passed to `HighchartsReact`.fixEnsure `import Highcharts from 'highcharts';` is present, Highcharts modules are applied (`MyModule(Highcharts);`), and `<HighchartsReact highcharts={Highcharts} options={options} />` is used correctly. -
Highcharts is not defined
cause The core Highcharts library was not imported, or its global presence (if not using the prop) was not guaranteed, especially in older CJS setups or non-modular environments.fixAlways explicitly import `Highcharts` from the `highcharts` package and pass it via the `highcharts` prop to `HighchartsReact`. Avoid relying on `Highcharts` being globally available via `window`. -
TypeError: can't access property "setHighcharts", etc. with Highcharts CDN.
cause Attempting to use Highcharts from a CDN or non-module build in conjunction with the React wrapper, leading to module resolution or instance-sharing issues.fixFor modular React applications, install Highcharts via npm (`npm install highcharts`) and import it as a module (`import Highcharts from 'highcharts';`) rather than relying on CDN-loaded global Highcharts instances. -
Chart not updating when props or state change.
cause The `options` object passed to `HighchartsReact` was mutated directly instead of providing a new, immutable object, or the `oneToOne` update parameter was not correctly handled in older versions.fixAlways create a new `options` object (e.g., using spread syntax) when making changes that should trigger a chart update. In older versions of the wrapper, `oneToOne={true}` might have been required as a prop, though this is often default behavior now.
Warnings
- breaking The `highcharts-react-official` package has been deprecated in favor of the new, officially supported `@highcharts/react` package (v4+). New projects should use `@highcharts/react` for improved API, ESM support, and better developer experience. Migration involves uninstalling the old package, installing the new one, and updating import statements and component props.
- gotcha Highcharts mutates the chart `options` object internally. To ensure React's reconciliation works correctly and to avoid unexpected behavior, always pass immutable options objects or create new objects when updating chart properties.
- gotcha Highcharts modules (e.g., exporting, drilldown, boost) are not automatically loaded and applied. They must be explicitly imported and then initialized by calling the module function with the Highcharts core instance.
- gotcha Highcharts is designed to run in a browser environment with access to the `window` and `document` objects. Server-Side Rendering (SSR) frameworks like Next.js will encounter errors if Highcharts components or modules are imported and executed on the server without proper safeguards.
- breaking `highcharts-react-official` v3.x requires React `^16.8.0` and Highcharts `^6.0.0`. Attempting to use significantly newer React versions (e.g., React 18) or very old Highcharts versions might lead to compatibility issues or require specific configurations. The newer `@highcharts/react` package explicitly requires React 18 and Highcharts 12.2+.
Install
-
npm install highcharts-react-official -
yarn add highcharts-react-official -
pnpm add highcharts-react-official
Imports
- HighchartsReact
import HighchartsReact from 'highcharts-react-official';
import { HighchartsReact } from 'highcharts-react-official'; - Highcharts (core library)
const Highcharts = require('highcharts');import Highcharts from 'highcharts';
- Highcharts modules
import 'highcharts/modules/exporting';
import HighchartsExporting from 'highcharts/modules/exporting'; // Apply module to Highcharts instance if (typeof Highcharts === 'object') { HighchartsExporting(Highcharts); }
Quickstart
import React, { useRef, useEffect, useState } from 'react';
import Highcharts from 'highcharts';
import HighchartsExporting from 'highcharts/modules/exporting';
import HighchartsReact from 'highcharts-react-official';
// Initialize Highcharts modules if Highcharts object is available
if (typeof Highcharts === 'object') {
HighchartsExporting(Highcharts);
}
const MyChartComponent = () => {
const chartComponentRef = useRef(null);
const [options, setOptions] = useState({
title: { text: 'My Sample Chart' },
series: [{
type: 'line',
data: [1, 2, 4, 6, 8, 7, 5, 3, 2, 1]
}],
chart: {
height: 400,
width: 600
},
credits: { enabled: false },
exporting: { enabled: true }
});
// Example of updating data after some time
useEffect(() => {
const timer = setTimeout(() => {
setOptions(prevOptions => ({
...prevOptions,
series: [{
...prevOptions.series[0],
data: [5, 7, 9, 8, 6, 4, 2, 1, 3, 5]
}]
}));
}, 3000);
return () => clearTimeout(timer);
}, []);
return (
<div>
<h2>Highcharts with React</h2>
<HighchartsReact
highcharts={Highcharts}
options={options}
ref={chartComponentRef}
/>
<p>This chart demonstrates basic line series, module integration (exporting), and reactive updates via component state.</p>
</div>
);
};
export default MyChartComponent;