Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
array of ref return 0 length in react

I tried to make an array of ref using the callback function.

class A extends React.Component {

const arr=[1,2,3,4,5];
    constructor(props) {
        super(props);
        this.inputs = [];
    }

componentDidMount()
{
console.log(this.inputs, this.inputs.length, this.inputs[0]) // here length of inputs is 0 and inputs[0] is undefined
}

    render() {
        return(
<div>
arr.map((key, index) => (
            <Input 
                key={key} 
                ref={input => this.inputs[index] = input}
            />
<button type="button" onClick={this.inputs[index].current.focus()}>Focus</button>
//here is got error as inputs[0] is undefined
)
)
</div>
;
    }
}

I am facing two problem

  • I created the ref inside the array, the array has multiple refs but does not know how the array is formatted as the length of the array is 0
  • As the length of the array is 0, I can't call the ref using the array index.
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!