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

174
Views
setState is not updating the values immediately on UI

Helle everyone ,

I was working on react framework and was using a setState to set a value and display it on UI. But it is not immediately updating its value. whenever I call the computeTotalQty function to set the value, it sets the value but the change is not reflected in containerList function which updates the value on UI. It displays the value a step later. Same is the case with containerSetList. Whenever the user clicks the removeItem button, it doesn’t immediately remove the item from the screen, though it is removed from the containerSetList.

Code for it is:

const [containerSetList, setContainerSetList]=useState<containerSet[]>([])
  const [totalQty,setTotalQty]=useState(0);

  let tempQty=0;

const computeTotalQty= ()=>{
    containerSetList.map((item:any)=>{
      tempQty=tempQty+item.quantity.value;
    })
    setTotalQty(tempQty);
  }

const ContainerList=()=>{
    return(
      <div >
         <Text alignment="center">
           Total Containers: {containerSetList.length} {" "}
           Total { unitType == "unit" ? "units" : "Weight"} : {totalQty} {unitType == "unit"?"":"gms"}
         </Text>
        <Divider/>
        <Table headerRows={1} spacing="small" showDividers={true}>
          <TableRow>
            <TableCell>Container ID</TableCell>
            <TableCell>Quantity</TableCell>
            <TableCell></TableCell>
          </TableRow>
          {containerSetList.map((item:any)=>(
            <TableRow >
              <TableCell>{item.inventoryItem.id}</TableCell>
              <TableCell>{item.quantity.value}</TableCell>
              <TableCell>
                <Button type="secondary" onClick={(e:any)=>removeItem(item)} size="small">
                  <Icon tokens={CloseMedium} />
                </Button>
              </TableCell>
            </TableRow>
          ))}
        </Table>
        <Divider spacingAfter="medium"/>
      </div>
    )
  }
const removeItem=(props:any)=>{
    let indexToRemove= containerSetList.indexOf(props);
    containerSetList.splice(indexToRemove, 1);
    computeTotalQty();
  }

return (
    <div>
      {ContainerList()}
    </div>
  );

Can anyone please tell me how can I solve it?

about 4 years ago · Juan Pablo Isaza
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!