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.
npm install react-chartjs-2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a basic line chart using `react-chartjs-2`, including the necessary Chart.js component registrations for tree-shaking.
Upgrade to v5.1.0 or later which restored CommonJS support, or migrate your project to use ESM imports if possible. For versions 5.0.0, ensure your bundler (e.g., Webpack, Rollup) is configured to handle ESM, and use `import` statements only.
Ensure you are using `chart.js@^4.0.0` or higher with `react-chartjs-2@^5.0.0`. If you need Chart.js v3, you should use `react-chartjs-2` v4.x.
Always import and call `ChartJS.register(...)` for all necessary components (e.g., `CategoryScale`, `LinearScale`, `PointElement`, `LineElement`, `Title`, `Tooltip`, `Legend`) from `chart.js` before rendering any chart components.
Always install `chart.js` alongside `react-chartjs-2` and ensure their versions satisfy the peer dependency requirements specified in `react-chartjs-2`'s `package.json` (e.g., `chart.js: ^4.1.1` for `react-chartjs-2@5.x`).
Ensure that `data` and `options` props are new objects (or deeply immutable updates) when changes occur, so React detects prop changes and `react-chartjs-2` can re-render or update the Chart.js instance. Using `useMemo` or `useState` to manage these objects effectively is common.
Add `import { Chart as ChartJS, ... } from 'chart.js'; ChartJS.register(...);` at the top level of your application or component where charts are used.Ensure you import and register the corresponding controller from `chart.js`, for example `LineController` for the `Line` chart component: `ChartJS.register(LineController, ...);`.
For `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.
Run `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.