I’ve used the React component, if I console the object its showing the object key-value in it. But if I access it in Component its undefined
.
I have initialized the object like this,
let faeko = new Faeko({}, client)
After the user login, the faeko
object updated with occupants
. Its showed in console, because of Javascript’s live updation in logged values.
I used the useEffect and setTimeout for adding delay for updating the value in Component. But its not.
This is my GameFrame.jsx
. Its logging the occupants value. If its showing then It should render the occupants in DOM. But its not.
import React, { useEffect, useState } from "react";
export const GameFrame = (props) => {
console.log("TEST FAEKO ", props.game.data);
const [start, setStart] = useState({});
let timer;
useEffect(() => {
console.log("tested", props.game);
console.log("Props.game", props.game.data);
clearTimeout(timer);
timer = setTimeout(() => {
console.log("props gets updated!", props.game.data.occupants);
setStart(props.game.data);
}, 1000);
}, [props.game.data]);
return (
<div>
{/* {console.log("start ", start)} */}
{start.occupants !== undefined && start.occupants.length > 0 && (
// console.log("here updated", start.occupants) &&
<div>Occupants</div>
)}
</div>
);
};
What should I do, Its working in single tab. If I need to join the second user in the room I’ve created, its not rendering. Is it resolvable?
If you want to join a google meet, ask me!
I’ve wasted 5 days in this……
2
Answers
Based on your code and description, it seems that the issue is related to the asynchronous nature of the
props.game
object update. When you log theprops.game.data
in theuseEffect
hook, you may see the updated value because it logs the value immediately after it gets updated. However, when you try to access the start state variable that depends onprops.game.data
in the return statement, it may still have the initial value because the state update may not have happened yet.One way to fix this issue is to add a conditional check in the return statement to render the component only when the start state variable has been updated with the new
props.game.data
value.An updated version of your code with this check:
In this code, the if statement in the return statement checks if the start object is not empty. If it’s empty, it returns null to render nothing. Once the start object gets updated with the new
props.game.data
value, the component will be re-rendered with the updated value.Also note that I moved the timer declaration inside the
useEffect
hook to avoid potential issues with multiple timers running simultaneously, and added a cleanup function to clear the timeout on component unmount.looks like you are trying to change the gamestate component using the props.game.data but as objects are passed as reference if the props.game.data object is being updated outside of the GameFrame it wont re render so thats why i suggest using useeffect so you can pass a unique identifier as a prop to the GameFrame component
i suggest use this identification prop for gameframe