react-apexcharts icon indicating copy to clipboard operation
react-apexcharts copied to clipboard

Chart not updating in nextjs

Open rayanfer32 opened this issue 3 years ago • 1 comments

https://codesandbox.io/s/muddy-sun-bjlx5?file=/pages/Charts2.js

rayanfer32 avatar Nov 19 '21 05:11 rayanfer32

Try changing your updateChart like so:

const updateChart = () => {
    const newSeries = [1, 2, 3, 4, 5];
    // update a nested property of state (method 2)
    setChartState((prev) => {
      // prev.series[0].data = newSeries;
      // prev.series[0] = { ...prev.series[0], data: newSeries };
      // console.log(chartState);
      return {
        ...prev,
        series: [
          {
            ...prev.series[0],
            data: newSeries
          }
        ]
      };
    });
  };

Your previous approach didn't work because you changed a value inside an existing object instead of creating a new one. Within React this leads to unexpected behaviour. You only re-render, when you get new values. But is far too expensive, to "deep-check" objects. That's why JavaScript only checks for reference, when evaluating a === b with Objects or Arrays. When creating a new object, this will evaluate to false and cause an update.

ccssmnn avatar Jan 03 '22 08:01 ccssmnn