React Chart.js 2
react-chartjs-2 provides React components that wrap the popular Chart.js library, enabling declarative data visualization within React applications. It supports Chart.js v4 and v3 (though v4 is recommended) and React versions 16.8.0 through 19.0.0. The current stable version is 5.3.1. Releases appear to be driven by Chart.js and React compatibility updates, with minor versions released every few months, and patch fixes as needed. Its key differentiator is providing a native React component API for Chart.js, abstracting away direct Chart.js instance management, and offering a robust integration with the React component lifecycle for dynamic chart updates.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'register')
cause Chart.js components (scales, elements, plugins) were not explicitly registered.fixAdd `import { Chart as ChartJS, ... } from 'chart.js'; ChartJS.register(...);` at the top level of your application or component where charts are used. -
Error: "Line" is not a registered chart type.
cause The specific chart type component (e.g., LineController) was not registered with Chart.js.fixEnsure you import and register the corresponding controller from `chart.js`, for example `LineController` for the `Line` chart component: `ChartJS.register(LineController, ...);`. -
Module not found: Error: Can't resolve 'react-chartjs-2' in '...'
cause CommonJS imports were used in a project configured for pure ESM when using `react-chartjs-2@5.0.0`, or vice-versa, or the package isn't installed.fixFor `react-chartjs-2@5.0.0`, convert `require()` to `import`. For any version, ensure `react-chartjs-2` and `chart.js` are correctly installed and that the import paths are correct. If using Webpack 4 with v5.x, ensure you're on at least v5.2.0 which restored compatibility. -
Unhandled Rejection (Error): Chart.js must be installed to use react-chartjs-2.
cause The peer dependency `chart.js` is not installed or its version is incompatible.fixRun `npm install chart.js` or `yarn add chart.js` and ensure the installed version satisfies the peer dependency range of your `react-chartjs-2` version.
Warnings
- breaking Version 5.0.0 of `react-chartjs-2` dropped CommonJS support entirely and became an ESM-only package.
- breaking Version 5.0.0 introduced support for Chart.js v4. This meant dropping support for Chart.js v3 as a direct peer dependency, though the README states it supports both.
- gotcha Chart.js v3+ introduced a tree-shaking friendly architecture where scales, elements, and plugins must be explicitly imported and registered with Chart.js.
- gotcha `react-chartjs-2` relies heavily on its peer dependency `chart.js`. Mismatched versions between `react-chartjs-2` and `chart.js` are a frequent source of errors.
- gotcha When dynamically updating chart options or data, it's crucial to ensure React's reconciliation process properly triggers Chart.js updates. Simply mutating objects passed as props might not work as expected.
Install
-
npm install react-chartjs-2 -
yarn add react-chartjs-2 -
pnpm add react-chartjs-2
Imports
- Line
const Line = require('react-chartjs-2').Line;import { Line } from 'react-chartjs-2'; - Chart as ChartJS
import { Chart as ChartJS } from 'chart.js'; - CategoryScale
import { CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend } from 'chart.js';
Quickstart
import React from 'react';
import { Line } from 'react-chartjs-2';
import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend } from 'chart.js';
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend);
export const MyLineChart = () => {
const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: 'My First dataset',
fill: false,
lineTension: 0.1,
backgroundColor: 'rgba(75,192,192,0.4)',
borderColor: 'rgba(75,192,192,1)',
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: 'rgba(75,192,192,1)',
pointBackgroundColor: '#fff',
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: 'rgba(75,192,192,1)',
pointHoverBorderColor: 'rgba(220,220,220,1)',
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [65, 59, 80, 81, 56, 55, 40]
}
]
};
const options = {
responsive: true,
plugins: {
legend: {
position: 'top'
},
title: {
display: true,
text: 'Chart.js Line Chart'
}
}
};
return <Line data={data} options={options} />;
};