Skip to content

Using with Next.js

react-chartjs-2 works in Next.js as in any React app. In the App Router, the only rule is where the chart lives: in a Client Component.

The components of react-chartjs-2 use React hooks and draw on a <canvas> in the browser, so they work only in Client Components. A chart rendered by a Server Component fails in next dev with this error:

TypeError: useRef only works in Client Components. Add the "use client" directive at the top of the file to use it.

next build fails too, when it prerenders the page, with an error such as TypeError: (0 , dk.useRef) is not a function.

Put the chart in a file of its own with the "use client" directive at the top, and register the parts of Chart.js in the same file:

app/votes-chart.tsx
'use client';
import {
Chart as ChartJS,
BarElement,
CategoryScale,
LinearScale,
Tooltip,
type ChartData
} from 'chart.js';
import { Bar } from 'react-chartjs-2';
ChartJS.register(BarElement, CategoryScale, LinearScale, Tooltip);
export function VotesChart({ data }: { data: ChartData<'bar'> }) {
return (
<Bar
data={data}
options={{
plugins: {
tooltip: {
callbacks: {
label: item => `${item.formattedValue} votes`
}
}
}
}}
/>
);
}

A Server Component, such as a page, loads the data and renders the chart:

app/page.tsx
import { VotesChart } from './votes-chart';
import { getVotes } from './votes';
export default async function Page() {
const votes = await getVotes();
return (
<VotesChart
data={{
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{ label: 'Votes', data: votes }]
}}
/>
);
}

The page is still rendered on the server: its HTML has an empty <canvas>, and Chart.js draws the chart in the browser. So the chart needs no next/dynamic with ssr: false. Neither do plugins, unless they read window as soon as they are imported, as chartjs-plugin-zoom did before version 3: see Zoom and Pan.

ChartJS.register() in a Server Component, such as a page or a layout, runs only on the server. The build passes, but the browser never gets the scales and elements, and the chart throws an error such as:

Uncaught Error: "category" is not a registered scale.

Register Chart.js in the module of the client component, as votes-chart.tsx does, or in a module that it imports.

The props that a Server Component passes to a Client Component must be serializable. Data and most options are, but functions are not, and Chart.js options often have them: tooltip and tick callbacks, scriptable options. Options with a function, passed from a page, fail the build:

Error: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server". Or maybe you meant to call this function rather than return it.

Set such options in the client component, as VotesChart sets the tooltip callback, and pass only the data from the server.

The Pages Router has no Server Components: pages render on the server and run in the browser. react-chartjs-2 works there as in any React app, without "use client".