Search code examples
apexcharts

Can I use the this keyword inside the zoomed callback in apexcharts?


Sorry this could be a trivial Javascript callback related question. When I zoom I want to call a function from the parent props. Is it possible to do this anyway? Or send additional props to the ReactApexChart component?

class ParentComp extends React.Component {
render() {
    let options = {
      ...,
        events: {
          zoomed: function(chartContext, { xaxis, yaxis }) {
            //I want to call this.props.setStartTime(xaxis.min)
          },
        ....
        }
    }

    return (
      <div>
          <ReactApexChart options={options} series={series} type="area" height={350} />
      </div>
    );
  }
}

Solution

  • Use an arrow function to preserve this

    class ParentComp extends React.Component {
      render() {
        let options = {
          ...,
            events: {
              zoomed: (chartContext, { xaxis, yaxis }) => {
                this.props.setStartTime(xaxis.min)
              }
            }
        }
    
        return (
          <div>
              <ReactApexChart options={options} series={series} type="area" height={350} />
          </div>
        );
      }
    }