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

219
Visualizações
Checked Items is undefined on selection React JS

I'm trying to read all checked items and storying that value to selectedTransaction.

Issue is when I check the 1st item, it throws undefined error & for the selection of the 2nd Item it shows 1 array item in the console log.

But if I console log the selectedTransactions, it shows correct item values.

What I'm doing wrong here. Please help

  const [selectedTransaction, setSelectedTransaction] = useState<any>();

  const handleCheckboxOnChange = (
    transactionDetails: TransactionDetail,
    isCheckeds: boolean,
  ) => {

    const selectedTransactions = transactionsList.transactionDetails
      .filter((item: TransactionDetail) => item.isChecked === true);
    setSelectedTransaction(selectedTransactions);

    console.log(selectedTransactions); // Shows 1 selected Item 
    console.log(selectedTransaction); // Shows undefined 
})
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

State is not updated immediately (its asynchronous and batched), thats why you are getting undefined or the previous state

setSelectedTransaction(selectedTransactions);
console.log(selectedTransactions); // Shows 1 selected Item 
console.log(selectedTransaction); // Shows undefined 

Use a useEffect to console state when data is changed. Give the dependency array [selectedTransaction]

useEffect(()=>{
    console.log(selectedTransaction); 
}, [selectedTransaction]);
about 4 years ago · Santiago Gelvez 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