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

383
Views
how can I update the state variable content?

My code as below:

import { useEffect, useState } from "react";
import Meeting from "./util/Meeting";
export default function TestMeeting(){
    const [peerList, setPeerList] = useState();
    const [meeting, setMeeting] = useState();
    let peerName;
    let sUsrAg = navigator.userAgent;
    if (sUsrAg.indexOf("Edg") > -1) {
        peerName = "Edge";
    } else {
        if (sUsrAg.indexOf("Chrome") > -1) {
            peerName = "Chrome";
        } else {
            if (sUsrAg.indexOf("Firefox") > -1) {
                peerName = "Firefox";
            } else {
                if (sUsrAg.indexOf("Safari") > -1) {
                    peerName = "Safari";
                }
            }
        }
    }
    useEffect(() => {
        let temp = new Meeting();
        temp.setDebug(true);
        temp.on("data", param => {
            console.log("Data Event Recevied:" + JSON.stringify(param));
        })
        temp.on("initialPeerList", (newPeerList) => {
            console.log("initialPeerList event recevied");
            console.log(newPeerList);
            setPeerList(newPeerList);
        });
        temp.on("newPeer", (newPeer) => {
            console.log("newPeer event recevied");
            console.log(peerList);
        });
        temp.on("removePeer", socketId => {
            console.log("removePeer event recevied");
            
        });
        temp.on("stream", param => {
            console.log("A stream event received:" + JSON.stringify(param));
            
        })
        temp.join(peerName);
        setMeeting(temp);
    }, [peerName]);   
    return (
        <>
        </>
    )
}

The "initialPeerList" event is received first and the peerList state variable should be updated. After that, when the "newPeer"event is received, suppose the content of peerList state variable should not undefined, however, it does.

I have followed the page: useState set method not reflecting change immediately

However, it does not solve the problem.

Why is this like that and how would I be able to fix it?

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!