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

221
Views
¿Cómo recibir una matriz de entrada dinámica en react.js?

Estoy tratando de recibir una matriz de entrada dinámica pero document.querySelector se devuelve undefined

 {chapitreList.map((partItem,index) => { return <div> <input type="text" name='soldc' id='soldc' className ="form-control" placeholder={partItem.chapitreDepense} /> </div> } const show_element = (e) => { console.log(document.querySelector('#soldc')[0].value) }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Los problemas

  1. document.querySelector devuelve un solo nodo DOM, no una lista de nodos (como querySelectorAll NodeList
  2. En HTML no puede tener múltiples nodos con la misma ID
  3. En React, evite usar llamadas DOM nativas (document.querySelector, etc.)

La solución

En lugar de usar métodos imperativos para recuperar múltiples valores de entrada, use un estado declarativo para mantener sus entradas y actualizar el estado en caso de cambio.

 function App() { const chapitreList = [{ chapitreDepense: "1" }, { chapitreDepense: "2" }]; const [formData, setFormData] = useState(Array(chapitreList.length).fill("")); console.log(formData); // access your array return ( <> {chapitreList.map((partItem, index) => ( <div key={index}> <input onChange={(e) => setFormData((prev) => { prev[index] = e.target.value; return [...prev]; }) } type="text" name="soldc" className="form-control" placeholder={partItem.chapitreDepense} /> </div> ))} </> ); }

Leer más sobre pensar en React

about 4 years ago · Santiago Trujillo 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!