Skip to content

Need help converting firebase 7.15.1 syntax into firebase 9.6.1 syntax

I am new to Firebase and I am following a tutorial that is a little bit outdated. I need help converting the code that they have into the newer version of it because I believe that the syntax has changed. They are using firebase 7.15.1 and I need the code to be in 9.6.1.

Here is the 7.15.1 code:

useEffect(() =>{
   db.collection('posts').onSnapshot(snapshot => {
   setPosts(snapshot.docs.map(doc => doc.data()))
   })
}, []);

I need to convert this snippet into the newer 9.6.1 firebase syntax. I am aware that there is documentation on upgrading firebase versions but I can’t seem to get it still for this snippet. Thanks

Answer

The Firestore code for that snippet in v9/modular SDK syntax:

onSnapshot(collection(db, 'posts'), (snapshot) => {
  setPosts(snapshot.docs.map(doc => doc.data()))
})