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

107
Views
useEffect doesn't see updated array

Below is my code snippet that doesn't work as intended. The thing is the first console.log in upper useEffect shows that in fact the array has length 1 and it contains an item BUT it doesn't pass the if check. I assume that console.log is somehow asynchronous and it sees the updated state but the if doesn't. In that case I'm not sure how would I wait for the state to update because I thought that useEffect REACTS ON updated stated. Thank you in advance.

const [roomsCurrent, setRoomsCurrent] = useState([]);

useEffect(() => {

    console.log(roomsCurrent);

    if (roomsCurrent.length != 0) {
        console.log(roomsCurrent.length);
    }

}, [roomsCurrent]);

useEffect(async () => {
    let roomData = [];

    if (rooms != null) {

        await Promise.all(rooms['rooms'].map(async (room) => {
            let result = await fetchRoomCurrent(room['room_identifier']);
            roomData.push({
                "room_identifier": room["room_identifier"],
                "display_name": room["display_name"],
                "temperature": result["temperature"],
                "humidity": result["humidity"],
                "pressure": result["pressure"],
                "thermostat_state": result["thermostat_state"],
                "dryer_state": result["dryer_state"]
            });
        })).then(setRoomsCurrent(roomData));
    }

}, [rooms]);

EDIT: rooms obj

{
"rooms": [
    {
        "display_name": "Living room",
        "room_identifier": "living_room"
    },
    {
        "display_name": "Bathroom",
        "room_identifier": "bathroom"
    },
    {
        "display_name": "Kitchen",
        "room_identifier": "kitchen"
    }
]
}

EDIT: Changed original code, still no luck. Not sure how to loop asynchronously through all the rooms and update after all the cycles have passed.

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!