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?

3
  • Have you set a default variable? Progress = []. Commented Jul 26, 2017 at 12:21
  • 9
    Does anyone know how to do this with the React.createRef() API? Commented Sep 19, 2018 at 8:45
  • 1
    How would access this reference list in another function? say you want to refer to index 1, for instance!!! Commented Nov 30, 2018 at 0:35

2 Answers 2

13

Create the array in you constructor, like:

constructor(){
  super()

  this.Progress = []
}
Sign up to request clarification or add additional context in comments.

1 Comment

How would access this reference list in another function? say you want to refer to index 1, for instance!!!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.