I am fetching all of the documents in a collection for firebase cloud firestore. I am using useEffect hook to call a function that fetches all the documents and then set the state with an array of new documents but the state doesn't seem to be updating.
This is the code that fetches and updates the state:
const fetchBlogs = () => {
db.collection('blogs').get().then(querySnapshot => {
querySnapshot.forEach(doc => {
setBlogs([...blogs,{
title:doc.data().title,
imgURL: doc.data().imgURL,
desc: doc.data().desc
}])
});
});
}
useEffect(() => {
fetchBlogs();
},[]);
And this is the code that renders the blogs:
{
blogs && blogs.map(blog=>{
return(
<Article
key={blog.title}
title={blog.title}
imgurl = {blog.imgURL}
desc={blog.desc}
/>
)
})
}