Chartjs-React chart.js dynamic data update not rendering

1👍

getNewData is not bound to the class context, so the setState will fail. you can use an arrow function so it will inherit the enclosing one.

getNewData = () => {
        const min = 1;
        const max = 10;
        const rand = min + Math.floor(Math.random() * (max - min));
        this.setState({
            data: {
                datasets: this.state.data.datasets.map((item, index) => ({
                    ...item,
                    data: [...this.state.data.datasets[index].data, rand]
                }))
            }
        });
    }

Leave a comment