Chartjs-Refs in reactjs Component empty

0👍

From React docs:

When the ref attribute is used on a
custom class component, the ref object receives the mounted instance
of the component as its current.

So you will not receive a canvas but an instance of component when it will mount. That’s why I think when App componentDidMount is called the LineChart was not yet mounted that’s why current is still null.

Again

this.chartObject = this.chartRef.current.getContext('2d');

will not work because you will receive an instance of a component, not a canvas.

Leave a comment