Everytime I run the following code React seems to continue and not fill the CurrentDevice as I asked in setCurrentDevice. It just throws an undefined in the log when asked on the last line. It does seem to log the correct device details as I can see when the first console.log runs. How do I make react fill the state CurrentDevice before continuing?
const q = query(collection(db, 'DeviceDetails'), where('serialNo', '==', SerialNo))
const querySnapshot = await getDocs(q)
querySnapshot.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
setCurrentDevice(doc.data());
console.log(doc.data());
})
console.log(currentDevice);