Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
UseEffect hook with firebase query only render the initial empty array even after the array is not empty anymore
const ID = useSelector((state) => state.userDetail.userID);
const [postItem, setpostItem] = useState([]);

useEffect(() => {
const q = query(
collection(firestore, "latestPost"),
where("userID", "==", ID)
);
onSnapshot(q, (querySnapshot) => {
querySnapshot.forEach((doc) => {
postItem.push({ ...doc.data(), id: doc.id });
});
});
}, 

[]);

I wanted to read data from firebase and set my useState with an empty array and even after the data is in the array, the screen still renders an empty array, I need to manually refresh the screen to see the change.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You shouldn’t be pushing directly to postItem. You should be using setPostItem. Also I think you need to add postItem to the end of useEffect in the square brackets.

So maybe something like

useEffect(()=> {
  const q = query(
    collection(firestone, "latestPost"),
    where("userId", "==", ID)
  );

  onSnapshot(q, (querySnapshot) => {
      querySnapShot.forEach((doc) => {
        setPostItem([...postItem, {...doc.data(), id:doc.id}])
    });
  });
}, [postItem]);

Basically that means useEffect will run again once you've setPostItem and also because useState has changed it will refresh the page.

I think that was how it is designed. However someone please correct me if my logic is incorrect!

about 4 years ago · Juan Pablo Isaza Report

0

The only way that react knows that it needs to render again is if you setState. Mutating the existing array will not cause a rerender. Also, don't forget to return the unsubscribe function so your snapshot listener can be torn down when the component unmounts:

useEffect(() => {
  const q = query(
    collection(firestone, "latestPost"),
    where("userId", "==", ID)
  );
  const unsubscribe = onSnapshot(q, (querySnapshot) => {
    const newItems = querySnapshot.docs.map(doc => ({
      ...doc.data(),
      id: doc.id
    });
    setpostItem(newItems);
  });
  return unsubscribe;
}, []);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!