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

153
Vistas
use state after setState on the same function deatils not updating

hello i have a problem with my code i have function that filter the state of users (users is the state that contain all the users that added) but i need on the same onclick event that 1 i will delete the current object from the array (and i did it correctly)

2 to get this users and count all the number after the delete

but when i go to users forEach it not getting the updated state and get the old one only the second click will update.. so after deleteFlight function execute its update the users without the current number but when i start to use user.forEach it not give me the update state

  const [users, setUsers] = useState([]);
  const deleteFlight = (num) => {
    const afterFilter = users.filter((user) => {
      return user.numFlight !== num;
    });
    
     setUsers(afterFilter);
     alert("deleted")
  };
      <button
        onClick={() => {
          const inp = numFlightInp.current.value;

          deleteFlight(Number(inp));

          users.forEach((user) => {
            passengerCnt += user.passenger;
            totalFlightCnt++;
          });
          setFlightCnt2(totalFlightCnt);
          setPassengerCnt2(passengerCnt);

          if (!checkDuplicateFlight(Number(inp))) return alert("not found match");
        }}
      >
        delete
      </button>
about 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