React array of refs

你离开我真会死。 提交于 2020-05-11 04:49:46

问题


i have read in a post on Stackoverflow question about refs

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?


回答1:


Create the array in you constructor, like:

constructor(){
  super()

  this.Progress = []
}



回答2:


Progress array is not initialized, initialize it in constructor..



来源:https://stackoverflow.com/questions/45326832/react-array-of-refs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!