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

xaxis values not updated on setState in React

Open jashsayani opened this issue 4 years ago • 0 comments

xaxis values (chart.options.xaxis.categories) not updated on setState in React.

class ChartView extends React.Component {
  constructor(props) {
    super(props);

    const sharedOptions = MyCommonChartOptions;
    sharedOptions.chart.id = 'my-chart';

    this.state = {
      chart: {
        options: sharedOptions,
        series: []
      }
    };
  }

  componentDidMount() {
    fetch('https://api.domain.com/data/chartdata').then((response) => {
      response.json().then((parsedResponse) => {
        const { chart } = this.state;
        chart.series = parsedResponse.values; // WORKS! <<<
        chart.options.xaxis.categories = parsedResponse.dates; // Not updated in Chart! <<<

        this.setState({
          chart: chart
        });
      });
    });
  }

  render() {
    return (
      <Chart options={this.state.chart.options} series={this.state.chart.series} type="line" />
    );
  }
}

Going to a different React component view and coming back to this view shows the xaxis values correctly.

jashsayani avatar Apr 19 '21 04:04 jashsayani