反应参考数组 [英] React array of refs
本文介绍了反应参考数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i have read in a post on Stackoverflow question about refs
我们可以使用类似以下代码的东西来将ref数组分配给这样的不同输入:
that we can use something like the following code to assign an array of refs to different inputs like this:
<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 }}/>
但是当我尝试它时,它将返回此错误:
but when i try it, it returns this error:
Uncaught TypeError: Cannot set property '0' of undefined
它不起作用,我缺少什么吗?
and it doesn't work, am I missing something?
推荐答案
在构造函数中创建数组,例如:
Create the array in you constructor, like:
constructor(){
super()
this.Progress = []
}
这篇关于反应参考数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文