简体   繁体   English

React render函数防止每次调用时创建新的canvas元素-Charts.js

[英]React render function preventing creating new canvas element on each time invoked - Charts.js

I am using React, Charts.js and react-chart.js. 我正在使用React,Charts.js和react-chart.js。

I am making an API request which returns data that i am then visualizing. 我正在提出一个API请求,该请求返回我正在可视化的数据。 The problem I am having is that when the page initially loads it only displays one of the names, an then it renders again and the complete list of three names is shown. 我遇到的问题是,当页面最初加载时,它仅显示名称之一,然后再次呈现,并显示三个名称的完整列表。 The problem is that a new graph is created each time the render function is invoked. 问题在于,每次调用render函数时都会创建一个新图形。

Does anyone know if there is a way to prevent a new graph being created each time, but instead to overwrite the previous graph that was created? 有谁知道是否有一种方法可以防止每次创建新图形,而是覆盖以前创建的图形?

The canvas is being rendered twice, which I guess is expected the way the code is setup. 画布被渲染两次,我想这应该是代码设置的方式。 On the first canvas element it just has one name, but then on the second canvas it contains all three names 在第一个画布元素上,它只有一个名称,但是在第二个画布上,它包含所有三个名称。

Please see my code below: 请在下面查看我的代码:

var App = React.createClass({
  getInitialState: function() {
    return {
      names: [],
      isLoaded: false
    }
  },
  componentDidMount: function() {
    $.get(url, function(data) {
      this.setState({
        names: data,
        isLoaded: true
      })
    }.bind(this));
  },
  render: function() {
    return ( < div > {
      this.state.isLoaded ? < Graph names = {
        this.state.names
      }
      /> : <div>Still Loading... </div >
    } < /div>)
  }
});

var Graph = React.createClass({

  render: function() {
    var names = [];
    for (var i = this.props.artists.length - 1; i >= 0; i--) {
      names.push(this.props.names[i].name);
    }

    var chartData = {
      labels: names,
      datasets: [{
        data: goals
      }, {
        data: predictions
      }]
    };

    return <LineChart data = {chartData} width = "600" height = "250" / >
  }
});

I think the problem is not on React, but in ChartJS ... It's getting new information... but instead of updating ... it is inserting new canvas.. 我认为问题不在React上,而是在ChartJS中……它正在获取新信息……但不是更新……它正在插入新画布。

Try setting redraw . 尝试设置redraw

From the docs https://github.com/reactjs/react-chartjs 从文档https://github.com/reactjs/react-chartjs

if data passed into the component changes, points will animate between values using chart.js' .update(). 如果传递到组件的数据发生更改,则点将使用chart.js的.update()在值之间进行动画处理。 If you want the chart destroyed and redrawn on every change, pass in redraw as a prop. 如果您希望图表在每次更改时都销毁并重绘,请以传递重绘为道具。 For example <LineChart data={this.state.chartData} redraw /> 例如<LineChart data={this.state.chartData} redraw />

This sounds like intended behaviour of React -- it will always rerender when the state changes. 这听起来像React的预期行为-当状态更改时,它将始终重新呈现。 Your only options are to not bind your render to the state, or control if the update should get called. 您唯一的选择是不将渲染绑定到状态,或控制是否应调用更新。 Without knowing what you're trying to accomplish, the react-chart docs seem to indicate it won't rerender/redraw the initial Canvas element . 不知道您要完成什么, react-chart文档似乎表明它不会重新渲染/重新绘制初始的Canvas元素

If you want to manually control when the component will update, use the shouldComponentUpdate method. 如果要手动控制组件的更新时间,请使用shouldComponentUpdate方法。

Use shouldComponentUpdate() to let React know if a component's output is not affected by the current change in state or props. 使用shouldComponentUpdate()可以让React知道组件的输出是否不受当前状态或道具更改的影响。 The default behavior is to re-render on every state change, and in the vast majority of cases you should rely on the default behavior. 默认行为是在每次状态更改时重新呈现,并且在大多数情况下,您应该依赖默认行为。

shouldComponentUpdate: function(nextProps, nextState) {
  // return true to continue with update, false to stop it
  return this.props.names.length == 0 && nextProps.names.length > 0
},

This could have side effects, such as if you are depending on props.names elsewhere and want that updated. 这可能会有副作用,例如,如果您依赖其他地方的props.names并希望对其进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM