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

102
Visualizações
Issue with my React state not updating properly

I'm trying to update the state of my component when a button is clicked. However, when only one of the buttons is clicked, my input state reflects an object with key input but the value is an empty array when it should be an array of the names of the buttons. This only happens when I select only one of the buttons

Here is my handleClick function:

  const [input, setInput] = useState({input: []});
  const [arr] = useState([]);

  const handleClick = async (event) => {
    event.currentTarget.classList.toggle('selected');
    if (arr.includes(event.target.getAttribute('a-key'))) {
      arr.removeByValue(event.target.getAttribute('a-key'));
    } else {
      arr.push(event.target.getAttribute('a-key'));
    }
    console.log(arr);
    setInput({input: arr});
    console.log(input);
    props.setSelected(input);
  }
about 4 years ago · Santiago Gelvez
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