0

I was able to solve the question: Uncaught TypeError: Cannot read property 'showBarChart' of undefined in React

var that = this;
chart.tooltip.contentGenerator(function (d) {
      var html = "<div>";
      d.series.forEach(function(elem){
        Object.keys(data_obj).forEach(function(key_1) {
          var outer_obj = data_obj[key_1];
          if (outer_obj["key"] === elem.key) {
              that.showBarChart(elem.key);
              var expr = outer_obj["values"][0]["expr"];
              html += "<p>" + elem.key + "</p>";
              html += "<p>x = " + d.value + ", y = " + elem.value + "</p>";
          }
        });
      })
      html += "</div>";
      return html;
    });

However, the solution led to a very weird bug in nvd3 scatterChart when the tooltip does not go away on mouse out and just stays on the page.

enter image description here

How could I fix it?

Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87

1 Answers1

0

The issue was that in componentDidUpdate I was calling createScatterChart. I commented it out and it started to work fine. It was happening apparently because of React workflow that I found to be described in the question: Trace why a React component is re-rendering

componentDidMount() {
 this.createScatterChart()
}

 componentDidUpdate() {
 //this.createScatterChart()
}

Calling this.setState() within the component. This will trigger the following component lifecycle methods shouldComponentUpdate > componentWillUpdate > render > componentDidUpdate

Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87
  • I've gotta ask - why are you using NVD3 with React rather than React specific implementations of D3? – jeznag Dec 28 '17 at 00:19
  • Could you send some libraries of React-D3? I think there could be less code written by me if I use NVD3, so I am trying to do less work. Honestly, I am not completely sure, but that is what I am thinking for now. – Nikita Vlasenko Dec 28 '17 at 01:32
  • 1
    https://emeeks.github.io/semiotic/#/semiotic/examples http://nivo.rocks/#/ https://uber.github.io/react-vis/ http://formidable.com/open-source/victory/gallery/ – jeznag Dec 28 '17 at 02:41