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

171
Views
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
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!