13

我在 Stackoverflow 上读过一篇关于 refs 的问题

我们可以使用类似下面的代码将一个 ref 数组分配给不同的输入,如下所示:

<Progressbar completed={25} id="Progress1" ref={(input) => {this.Progress[0] = input }}/>

<Progressbar completed={50} id="Progress2" ref={(input) => {this.Progress[1] = input }}/>

<Progressbar completed={75} id="Progress3" ref={(input) => {this.Progress[2] = input }}/>

但是当我尝试它时,它会返回此错误:

Uncaught TypeError: Cannot set property '0' of undefined

它不起作用,我错过了什么吗?

4

2 回答 2

13

在构造函数中创建数组,例如:

constructor(){
  super()

  this.Progress = []
}
于 2017-07-26T12:21:40.423 回答
1

进度数组未初始化,在构造函数中初始化它..

于 2017-07-26T12:29:07.640 回答