NVD3工具提示错误

问题描述 投票:0回答:1

我能够解决这个问题: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;
    });

然而,这个解决方案在nvd3 scatterChart导致了一个非常奇怪的错误,当tooltip没有消失在mouse out并且只停留在页面上。

enter image description here

我该怎么办呢?

javascript reactjs nvd3.js
1个回答
0
投票

问题是在componentDidUpdate我叫createScatterChart。我评论了它,它开始工作正常。这显然是因为React工作流程,我发现在问题中描述: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

© www.soinside.com 2019 - 2024. All rights reserved.