GithubHelp home page GithubHelp logo

Comments (7)

ppotaczek avatar ppotaczek commented on June 27, 2024 1

Currently, the both contexts (React component and chart) in chart functions can be accessed by binding the right this, for example:

constructor(props) {
  this.state = {
    options: {
      chart: {
        events: {
          render: function() {
            const chart = this.chart;
            const component = this;
            ...
          }.bind(this)
        }
      },
      ...
    }
  };
}

chartCallback(chart){
  this.chart = chart;
}

render() {
  return (
    <HighchartsReact
      highcharts={Highcharts}
      options={this.state.options}
      callback={ this.chartCallback.bind(this) }
    />
  )
}

Live demo: https://stackblitz.com/edit/react-e7gcju?file=index.js

from highcharts-react.

KacperMadej avatar KacperMadej commented on June 27, 2024

You could access chart instance through context of chart's load event to call showLoading() and hideLoading() on it.

from highcharts-react.

chillyistkult avatar chillyistkult commented on June 27, 2024

@KacperMadej How do I access react context within this events? For example to check if this.props.loading is true?

events: {
   render() {
      if (this.props.loading) { // How?
         this.showLoading();
      } else {
         this.hideLoading();
      }
   },
},

from highcharts-react.

KacperMadej avatar KacperMadej commented on June 27, 2024

Have you tried fat arrow function? But then this will not be the chart, so you need to choose, or use a different event to store chart reference in react context.

from highcharts-react.

chillyistkult avatar chillyistkult commented on June 27, 2024

I tried a lot of things, including arrow function, but with no success. This problem unfortunately applies to any function I would like to use within Highchart context.

I think the right way to do it would be if Chart is just a parameter of the callback function, instead of providing it via this context.

events: {
   render: (chart) => {
      if (this.props.loading) {
         chart.showLoading();
      } else {
         chart.hideLoading();
      }
   },
},

from highcharts-react.

pawelfus avatar pawelfus commented on June 27, 2024

That's gonna break backward compatibility (events.render is a callback directly from Highcharts core), confusing much more users than react users. Probably this wrapper could take care of it, but why don't we simply..

Define:

var propLoading = this.props.loading;

Then use it:

events: {
   render: () {
      if (propLoading) {
         this.showLoading();
      } else {
         this.hideLoading();
      }
   },
},

Internal note:
In general, if we decide to replace context in render, then we should replace context for all callbacks to keep them consistent.

from highcharts-react.

chillyistkult avatar chillyistkult commented on June 27, 2024

In general, if we decide to replace context in render, then we should replace context for all callbacks to keep them consistent.

Please, it would make so much sense!

from highcharts-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.