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

218
Views
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 answers
Answer question

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 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!