NVD3工具提示错误

时间:2017-12-27 22:21:59

标签: javascript reactjs nvd3.js

我能够解决问题: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在[{1}}上没有消失并且只停留在页面上时,该解决方案在scatterChart tooltip中导致了一个非常奇怪的错误。

enter image description here

我该如何解决?

1 个答案:

答案 0 :(得分: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