I created an array using some data then I tried to copy it to the useState array. But it doesn’t work. Also, I viewed it using the console log. but line 49 logged before line 36. can I fix this?
Console -
Code -
const retrieveCompany = async () =>{
try{
const colletionRef = collection(db, 'corporations');
const q = query(colletionRef, orderBy('createdAt', "desc"));
const unsub = onSnapshot ( q,(snapshot) => {
setCorporations([]);
let list = [];
snapshot.docs.forEach( async (doc) => {
let newItem = {id: doc.id, ...doc.data()};
if(newItem.poc) {
let userData = await getDoc(newItem.poc);
console.log(userData.data())
newItem = {pocUser: userData.data(), ...newItem}
console.log(newItem);
list.push(newItem);
}
else{
list.push(newItem);
}
// await getPOSUser(doc.data().poc);
// list.push({ id: doc.id, ...doc.data()});
})
console.log(list)
setCorporations(list);
console.log(corporations)
});
return () => {
unsub();
}
}catch(err){
console.log(err);
}
}

