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

147
Visualizações
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 à 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