Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

A gradient fill in react-chartjs-2 throws or never paints

Your error

TypeError: Cannot read properties of undefined (reading 'map')3 more lines it can show

Fix it fast

Most common cause, 1 of 3

data is still passed as a function of the canvas

Pass a plain object and move the gradient into the dataset's backgroundColor as a function.

Fix
// before (react-chartjs-2 2.x and 3.x)
// <Line data={(canvas) => ({ datasets: [{ backgroundColor: makeGradient(canvas.getContext('2d')) }] })} />

// after
const data = {
  labels: ['Mon', 'Tue', 'Wed'],
  datasets: [{
    label: 'Visits',
    data: [12, 19, 7],
    fill: true,
    backgroundColor: (context) => areaGradient(context.chart),
  }],
};

<Line data={data} />
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

A gradient fill in react-chartjs-2 4 and 5 fails in three ways: a data prop written as a function, removed in v4, ends in Cannot read properties of undefined (reading 'map'); a gradient built before the chart is laid out has no chartArea; and fill does nothing without the Filler plugin. Use a scriptable backgroundColor and register Filler. Applies to react-chartjs-2 5.3, Chart.js 4.6.

  • Applies to react-chartjs-2 5.3.1, Chart.js 4.6.1, React 19.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Look at what the data prop receives and whether Filler is registered. A function in data is cause 1. A gradient built from chartRef.current or chart.chartArea outside a scriptable option is cause 2. No Filler in the register call, while the chart shows no error, is cause 3.

Run this first
grep -rn "data={" src/ | grep "=>"
grep -rn "createLinearGradient\|chartArea\|chartRef.current" src/
grep -rn "ChartJS.register\|Chart.register" src/

Then find the line you see in this list and open its fix:

  1. 1data is still passed as a function of the canvasCannot read properties of undefined (reading 'map'), with setDatasets or cloneData from react-chartjs-2 in the stack, and data={(canvas) => ...} in the component
  2. 2The gradient is created before the chart has a layoutCannot read properties of null (reading 'ctx') from chartRef.current during render, or reading 'bottom' of an undefined chartArea on the first draw
  3. 3The Filler plugin is not registeredNo error at all: the line takes the gradient colour but the area under it stays blank even with fill: true

What does the error look like?

  • TypeError: Cannot read properties of undefined (reading 'map')
  • TypeError: Cannot read properties of null (reading 'ctx')
  • TypeError: Cannot read properties of undefined (reading 'bottom')
  • The line draws but the area under it stays empty

Cause 1 of 3

data is still passed as a function of the canvas

You seeCannot read properties of undefined (reading 'map'), with setDatasets or cloneData from react-chartjs-2 in the stack, and data={(canvas) => ...} in the component

Check

Passing data as a function of the canvas throws inside react-chartjs-2 4 and 5. The v4 migration guide says that option was removed; the component now copies data.labels and data.datasets straight from the prop, and a function has no datasets array, so the map call on it fails before Chart.js starts.

Check
grep -rn "getContext('2d')" src/

Fix

Pass a plain object and move the gradient into the dataset's backgroundColor as a function. Chart.js calls that function with a context whose chart property holds ctx and chartArea.

Fix
// before (react-chartjs-2 2.x and 3.x)
// <Line data={(canvas) => ({ datasets: [{ backgroundColor: makeGradient(canvas.getContext('2d')) }] })} />

// after
const data = {
  labels: ['Mon', 'Tue', 'Wed'],
  datasets: [{
    label: 'Visits',
    data: [12, 19, 7],
    fill: true,
    backgroundColor: (context) => areaGradient(context.chart),
  }],
};

<Line data={data} />

Verify

The chart mounts without a TypeError, and the console shows no error from cloneData.

Verify
console.log(typeof data) // "object"

Confirmed by react-chartjs-2.js.org

Did this fix it?

Cause 2 of 3

The gradient is created before the chart has a layout

You seeCannot read properties of null (reading 'ctx') from chartRef.current during render, or reading 'bottom' of an undefined chartArea on the first draw

Check

A canvas-context error while building the gradient means the code ran before Chart.js finished its layout. During the first render the ref is still null, and the Chart.js linear gradient sample notes that chartArea is missing on the initial chart load, so reading its edges fails.

Check
grep -rn -B2 -A6 "createLinearGradient" src/

Fix

Build the gradient inside the scriptable option, return nothing until chartArea exists, and rebuild it only when the chart's size changes, as the official sample does.

Fix
let cached, cachedWidth, cachedHeight;

function areaGradient(chart) {
  const { ctx, chartArea } = chart;
  if (!chartArea) return; // first pass, before layout
  const width = chartArea.right - chartArea.left;
  const height = chartArea.bottom - chartArea.top;
  if (!cached || width !== cachedWidth || height !== cachedHeight) {
    cachedWidth = width;
    cachedHeight = height;
    cached = ctx.createLinearGradient(0, chartArea.bottom, 0, chartArea.top);
    cached.addColorStop(0, 'rgba(54, 162, 235, 0)');
    cached.addColorStop(1, 'rgba(54, 162, 235, 0.6)');
  }
  return cached;
}

Verify

Reload and resize the window: the fill fades from bottom to top, and no TypeError appears on the first paint.

Verify
// resize the browser window and watch the console

Confirmed by chartjs.org

Did this fix it?

Cause 3 of 3

The Filler plugin is not registered

You seeNo error at all: the line takes the gradient colour but the area under it stays blank even with fill: true

Check

When the gradient never fills the area but nothing throws, Chart.js is missing the plugin that draws fills. react-chartjs-2 4 and 5 are tree-shakable like Chart.js 3 and 4, so every element, scale and plugin must be registered, and the react-chartjs-2 FAQ names Filler as the one that background filling needs.

Check
grep -rn "Filler" src/

Fix

Register Filler next to the elements and scales the chart uses. The typed Line component already registers its own controller.

Fix
import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Filler } from 'chart.js';
import { Line } from 'react-chartjs-2';

ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Filler);

Verify

The area under the line now shows the gradient from cause 2.

Verify
grep -rn "Filler" src/

Confirmed by react-chartjs-2.js.org

Did this fix it?

How do I stop it coming back?

  • Keep gradient code in one helper that takes the chart, so every chart guards chartArea the same way.
  • Register Chart.js parts in a single module imported once, and add Filler there as soon as any chart uses fill.
  • When copying an example, check its react-chartjs-2 major: data as a function belongs to 2.x and 3.x code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.