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

175
Visualizações
In React with Socket.io: Calling function inside useEffect don't give me the actual variable

When i call my function getNextCard() directly with <button onClick={getNextCard}>NextCardRound</button></div> it works, gives me the right result everytime. But when i try to call it from socket.on('end-turn') it don't give me the updated round value (that comes from socket.on('get-next-card')). How can i get the updated value from round? Thank you

useEffect(() => {
    console.log('useEffect: ' + socket);
  //ALL EVENTS = ROOM EVENTS
  //LISTENERS FOR COMMON EVENTS LIKE TABLE CARDS:
  socket.on('get-next-card', (tablecards, tblcards, nextround) => {
    console.log('get-next-card: ', tablecards, tblcards, nextround);
    console.log("ESSE É O ROUND Q CHEGA DO BACK PARA ATUALIZAR: ", nextround);
    if(nextround!=='showdown'){
    //let newArray = tablecards.concat(tblcards);
    setRound(nextround);
    setTableCards([...tablecards, ...tblcards]);
    }
    })

    socket.on('end-turn', () => {
      console.log('end-turn: ', round);
      
        getNextCard();
    
    })
    }) ```
    ```
function getNextCard(){
    console.log("ESSE É O ROUND ANTES E Q VAI SER ENVIADO P BACK: "+round);
    socket.emit('next-card', tablecards ,round, room, (cb)=>{
      console.log("get next card function ", cb);
    })
  }
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