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

151
Visualizações
React JS - Select, Why I'm getting double values printed and stored in the database

Okay I have a question why when someone clicks on multiple selects answers it does store all the value instead of only storing the last value ?

So let's say I have a select with 2 options only

enter image description here

And then I click on option 1 but decided to change to option 2 at last minute why does it save both data/information in the database ?

enter image description here

It prints like this in the table: enter image description here

As you can see it does print both schools ("colegios") because I clicked both of them in the select.

This is the snip of code that is relevant, let me know if need full code.

const [idType1, setidType1] = useState([]);

const register = (e) => {
        e.preventDefault();
        const docRef = db.collection('usuarios').doc(user.uid).collection('estudiantes').doc();
        docRef.set({
            
          name: firstName + " " + lastName,
          school: idType1,
          grade: idType2,
          uid: docRef.id,

        }).then((r) => {
            history.push("/Inicio");
        })
      }

<select className = "crear_escuela" onChange = {(e) => setidType1([...idType1, e.target.value])} required>
   <option value="" disabled selected hidden>Seleccione...</option>
   <option value = 'Academia Internacional de Boquete' label = 'Academia Internacional de Boquete' />
   <option value = 'Academia Internacional de David' label = 'Academia Internacional de David' />
</select>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is in onChange = {(e) => setidType1([...idType1, e.target.value]).

Every time the value in the select changes, you add the selected value to idType1. Given that this is a dropdown, you'll typically want to replace the existing value of idType1 with the newly selected value.

So:

onChange = { (e) => setidType1(e.target.value) }
about 4 years ago · Juan Pablo Isaza 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