14

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?

Elias Ghali
  • 823
  • 1
  • 13
  • 29

2 Answers2

13

Create the array in you constructor, like:

constructor(){
  super()

  this.Progress = []
}
CD..
  • 72,281
  • 25
  • 154
  • 163
  • 1
    How would access this reference list in another function? say you want to refer to index 1, for instance!!! – dryleaf Nov 30 '18 at 00:36
1

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

Asim Khan
  • 1,969
  • 12
  • 21