React Google Charts Wrapper
react-google-charts is a fully typed React component library that provides a lightweight wrapper around the official Google Charts library. It simplifies the integration of over 25 different Google Chart types, supporting animations and extensive customization options. The package is currently at version 5.2.1 and shows an active release cadence with frequent updates, including new features, bug fixes, and typings enhancements, as evidenced by recent 5.x releases. Its primary differentiator is providing a modern React component API with TypeScript support, abstracting away the direct interaction with the Google Charts loader script and API, making it easier for React developers to quickly add data visualizations to their applications without deep knowledge of the underlying Google Charts library.
Common errors
-
TypeError: (0 , react_google_charts__WEBPACK_IMPORTED_MODULE_2__.Chart) is not a function
cause Attempting to use `Chart` as a default import when it is a named export, especially in projects configured for ESM.fixChange your import statement from `import Chart from 'react-google-charts';` to `import { Chart } from 'react-google-charts';`. -
Error: google.charts.load() called multiple times with different settings.
cause Multiple instances of `react-google-charts` or other libraries are trying to load the Google Charts loader with conflicting settings. This often happens in larger applications or micro-frontends.fixEnsure that `google.charts.load` is called only once with a consistent configuration across your application. You might need to coordinate loading or use a single top-level `Chart` component to manage the loader. -
Error: [react-google-charts]: A 'chartType' prop is required.
cause The `Chart` component was rendered without specifying the `chartType` prop, which is mandatory for Google Charts to know which type of chart to render.fixAlways provide a valid `chartType` prop, e.g., `<Chart chartType="BarChart" ... />`. -
Warning: Failed prop type: Invalid prop `data` supplied to `Chart`. Expected an array of arrays.
cause The `data` prop provided to the `Chart` component is not in the expected Google Charts DataTable format (an array where the first element is headers and subsequent elements are rows).fixEnsure your `data` prop is an array of arrays, with the first inner array typically being the column headers and subsequent arrays being the data rows, e.g., `data={[['x', 'y'], [1, 2], [3, 4]]}`.
Warnings
- breaking Version 4.0.0 dropped support for UMD bundles and older browsers (e.g., Internet Explorer). The package now primarily uses native ES Modules (ESM).
- gotcha Prior to v5.1.0, event listeners were not always correctly added and removed, potentially leading to memory leaks or incorrect behavior when props changed or components unmounted.
- gotcha In versions prior to 5.0.0, charts might not re-draw correctly when data, columns, or rows props changed, leading to stale visualizations.
- gotcha Some versions experienced an infinite loading issue while the Google Charts script was being loaded.
- deprecated The `charteditor` package was removed from default `chartPackages` in version 3.0.6, and docs were updated accordingly.
Install
-
npm install react-google-charts -
yarn add react-google-charts -
pnpm add react-google-charts
Imports
- Chart
const Chart = require('react-google-charts').Chart; // Incorrect for modern React apps, especially post v4.0.0 import Chart from 'react-google-charts'; // Common mistake, Chart is a named exportimport { Chart } from 'react-google-charts'; - GoogleChartWrapper
import GoogleChartWrapper from 'react-google-charts'; // Not a default export
import { GoogleChartWrapper } from 'react-google-charts'; - ChartEvent
import type { ChartEvent } from 'react-google-charts';
Quickstart
import React from 'react';
import { Chart } from 'react-google-charts';
function MyChartComponent() {
const data = [
["Year", "Sales", "Expenses"],
["2004", 1000, 400],
["2005", 1170, 460],
["2006", 660, 1120],
["2007", 1030, 540]
];
const options = {
title: "Company Performance",
curveType: "function",
legend: { position: "bottom" }
};
return (
<div style={{ width: '100%', maxWidth: '800px', margin: '0 auto' }}>
<h2>Sales & Expenses Over Time</h2>
<Chart
chartType="LineChart"
data={data}
options={options}
width="100%"
height="400px"
legendToggle
/>
</div>
);
}
export default MyChartComponent;