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

236
Views
Get Value of array name in react js in functional componet

How can I get value with input name array in functional component based react js.

Here is my code

<form method="post" onSubmit={handleSubmit}>
   <input type="text" name="otp[]"  placeholder="0" className="form-control inputs"/>
   <input type="text" name="otp[]"  placeholder="0" className="form-control inputs"/>
   <input type="text" name="otp[]"  placeholder="0" className="form-control inputs"/>
   <input type="text" name="otp[]"  placeholder="0" className="form-control inputs"/>
   <button className="btn btn-default text-light pos-rel">Verify</button>
 </form>

Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should store input values with state, for this i suggest an object.

const [values, setValues] = useState({otp1: 0, otp2: 0, ...restOfTheInputs });

Then you can trigger a change of state when an input is changed by adding a onChange prop to your inputs. (You should also give these inputs a unique name)

<input type="text" name="otp1" value={values.opt1} onChange={(e) => handleInput(e)} />

in this function you will want to update state with the new value for the input replacing any old value while keeping any values from the other inputs.

const handleInput = (event) => {
    const {name, value} = event.target;
    setValues(currentValue => ({...currentValue, ...{name: value}}))
}
about 4 years ago · Juan Pablo Isaza Report
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!