0

Here I am retrieving the answer doc from firebase for every question which is passed in this component. But state is not getting updated in use Effect hook. Contents of tempArray are not getting inserted into getAns.When I try printing tempArray it is printing the data it got from firebase.But when I tried to print getAns it's showing empty array.I'll be really thankful if someone can tell that why getAns state is not getting updated

function Post({questionData, userData}) {
   const currQuesId = (questionData?.quesId);
   const [getAns, setGetAns] = useState([]);
   useEffect(()=>{
      if(currQuesId){
      (async()=>{
       const q = query(collection(db, 
      `questions/${currQuesId}/answers`));
       const querySnapshot = await getDocs(q);
       let tempArray = [];
       querySnapshot.forEach((doc)=>{
          tempArray.push(doc.data());
        })

       setGetAns([...tempArray]);
        console.log(currQuesId);
        //This is printing blank array in console
        console.log(getAns);
        //This is printing the doc retreived from firebase
        console.log(tempArray);
     })();
   }
 }

0 Answers0