Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
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?

over 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda