Chartjs-Accessing conditionally rendered canvas element in React for plotting

1👍

It doesn’t work because there are no further state updates after you assign the ref to the canvas; that means no more re-renderings (calls to render method) so you never get to execute this.chart(). Instead of calling that last method in render, add it to the setState callback in fakeAPICall:

this.setState({
    data: [1,2,3],
    labels: ['A', 'B', 'C']
}, () => this.chart());

The callback will be executed once setState is completed and the component is re-rendered. You can read more about state callbacks here.

Leave a comment