简体   繁体   English

React JS-Tick回调传递对象参数

[英]React JS - Tick Callback passing object argument

I have a scatter graph and a callback function calling mapValtoString to define the tick value of the x axis. 我有一个散点图和一个调用mapValtoString的回调函数来定义x轴的刻度值。 I need to pass dendoLabels to that function which will be the "liste" argument of the function. 我需要将dendoLabels传递给该函数,该函数将成为该函数的“ liste”参数。

In the code below, datas and dendolabels are empty to make it easier to read. 在下面的代码中,数据和dendolabels为空,以便于阅读。

When I try to pass dendolabels to the function, it pass an undefined object. 当我尝试将dendolabels传递给函数时,它传递了未定义的对象。 I absolutely don't understand why it can't reach it. 我绝对不明白为什么它无法达到目标。

function mapValtoString(val,liste){

    for (var i = 0; i < Object.keys(liste).length ; i++) {
      if (liste[i]["x"] == val) {
        return liste[i]["label"]
      }

    }
        return val
};


class Dashboard extends React.Component {
 constructor(props) {
   super(props);
   this.state = {
    bigChartData: "data1",
    dendo: [],

 };
};

dendoLabels =  [{x:"5", label:"Test"}]
chartExample3 = {
data: canvas => {
  let datass = []
  console.log(this.dendoLabels)
  for (var i = 0; i < Object.keys(this.state.dendo).length; i++) {
    if (this.state.dendo[i]["Label1"] != "") {this.dendoLabels.push({x:this.state.dendo[i]["x1"], label: this.state.dendo[i]["Label1"]})}        
    if (this.state.dendo[i]["Label2"] != "") {this.dendoLabels.push({x:this.state.dendo[i]["x3"], label: this.state.dendo[i]["Label2"]})}

      datass.push({
        borderColor: "#d048b6",
        borderWidth: 2,
        borderDash: [],
        showLine: true,
        borderDashOffset: 0.0,
        data:[{x: this.state.dendo[i]["x1"],y: this.state.dendo[i]["y1"], index:this.state.dendo[i]["Label1"]},{x: this.state.dendo[i]["x2"],y: this.state.dendo[i]["y2"]}]},

        {
        borderColor: "#d048b6",
        borderWidth: 2,
        borderDash: [],
        showLine: true,
        borderDashOffset: 0.0,
        data:[{x: this.state.dendo[i]["x3"],y: this.state.dendo[i]["y3"]},{x: this.state.dendo[i]["x4"],y: this.state.dendo[i]["y4"]}]},

        {
        borderColor: "#d048b6",
        borderWidth: 2,
        borderDash: [],
        showLine: true,
        borderDashOffset: 0.0,
        data:[{x: this.state.dendo[i]["x2"],y: this.state.dendo[i]["y2"]},{x: this.state.dendo[i]["x3"],y: this.state.dendo[i]["y3"], index:this.state.dendo[i]["Label2"]}]
      })
  }
   return{
    datasets: datass,
  }
},

options: {
  legend: false,
  fontSize: 30,
  color: "#666",
  maintainAspectRatio: false,
  responsive: true,
  scales: {
        xAxes: [{
            ticks: {
                display: true,
                stepSize: 5,
                callback: function(value,index,values){
                      console.log(this.dendoLabels)
                      return mapValtoString(value,this.dendoLabels)
                  },

            }
        }]
    }
}

}; };

dendoLabels is in the format of the code below which is working by the way, you will find the result in the image below. dendoLabels采用下面的代码格式,该代码可以正常工作,您将在下面的图像中找到结果。

ticks: {
 display: true,
 stepSize: 5,
 callback: function(value,index,values, dendoLabels = [{x:"5", label:"It works"}]){
 return mapValtoString(value,dendoLabels)
 },
}

Result 结果

How can I pass my dendoLabel object to that function ? 如何将dendoLabel对象传递给该函数?

The following code "works" because dendoLabels is not passed to the callback when it's invoked and the default value is used instead. 以下代码“有效”,因为dendoLabels在调用时未传递给回调,而是使用默认值。

ticks: {
 display: true,
 stepSize: 5,
 callback: function(value,index,values, dendoLabels = [{x:"5", label:"It works"}]){
 return mapValtoString(value,dendoLabels)
 },
}

The problem is that the dendoLabels variable in living inside the chartExample3.data() scope and out of the callback's reach. 问题在于dendoLabels变量位于chartExample3.data()范围内,并且不在回调的范围之内。 The solution is to use a closure. 解决方案是使用闭包。

First, remove the dendoLabels from the callback's arguments. 首先,从回调的参数中删除dendoLabels

callback: function(value,index,values)

Now define dendoLabels as a variable in the same scope that chartExample3 currently lives in. 现在,将dendoLabels定义为chartExample3当前所在范围内的变量。

Like this. 像这样。

let dendoLabels = [{x:"5", label:"It works"}]

chartExample3 = {
  data: canvas => {
  let datas = []
  return{
    datasets: datas,
  }
},
options: {
  legend: false,
  fontSize: 30,
  color: "#666",
  maintainAspectRatio: false,
  responsive: true,
  scales: {
        xAxes: [{
            ticks: {
                display: true,
                stepSize: 5,
                callback: function(value,index,values){
                return mapValtoString(value,dendoLabels)
                },
            }
        }]
    }
  }
}

EDIT because of new requirement . 由于新的要求而进行编辑 Setting dendoLabels as a property of React component. 将dendoLabels设置为React组件的属性。

If you want to use this.dendoLabels you must add it as a property of that class and you must assure that the callback is invoked within the right context. 如果要使用this.dendoLabels ,则必须将其添加为该类的属性,并且必须确保在正确的上下文中调用该回调。 It isn't clear from the code that you posted what is and isn't part of the class and how the callback is invoked. 从代码中还不清楚您发布了什么是类,什么不属于类以及如何调用回调。

class Dashboard extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      bigChartData: "data1"
      , dendo: []
    }
  }

  dendoLabels = [{
    x: "5"
    , label: "Test"
  }]
}

Try using an arrow function () => {} rather than function() {} . 尝试使用箭头函数() => {}而不是function() {}

You can read more on the difference between them here . 您可以在此处详细了解它们之间的区别。

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

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