Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
How to receive array from dynamic input in react.js?

I am trying to receive an array from dynamic input but document.querySelector is returned 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 Respostas
Responde à pergunta

0

The issues

  1. document.querySelector returns a single DOM node, not a NodeList (like querySelectorAll does.
  2. In HTML you cannot have multiple nodes with the same ID
  3. In React, avoid using native DOM calls (document.querySelector, etc.)

The solution

Instead of using imperative methods of retrieving multiple input values, use a declarative state to hold your inputs and update the state on change.

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>
      ))}
    </>
  );
}

Read more about thinking in React

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda