I have an issue that is kind of hard to explain but ill try my best. I have a chat app, that displays users you can talk to and when you click on a user it fetches the messages and sets a selected user on click using the useContext. Everything works when you click a user. (All the messages gets fetched). However, the user context isn't set on the first click but it works on the second click.
this is the function that's sets everything:
const handleUserClick = (messsagesID:string, selectedUserInfo:selectedUserProps)=>{
setSelectedUserMessages(selectedUserInfo);
console.log(selectedUserInfo);
selectMessage(messsagesID);
showMessage();
}
This is the context function:
const setSelectedUserMessages=(selectedUser:selectedUserProps|undefined)=>{
setSelectedUser(selectedUser);
}
this is the chat list loop:
{
Object.entries(messages!)?.map((chat:any)=>(
<div key={chat[1].userInfo.uid} className="" onClick{()=>handleUserClick(chat[0],chat[1].userInfo)}>
<UserMessage name={chat[1].userInfo.displayName} photoUrl={chat[1].userInfo.photoUrl} message={chat[1].lastMessage.message} />
</div>
))
}
When I console log the data. Its correct I get the information back on the first click but when I try and set it to the context its empty. But on the second click it gets set correctly. This is very strange as everything else gets set as it should on the first click.
Hopefully I explain this correctly and it isn't confusing.