Home > Enterprise >  How to convert this firebase code into its latest syntax in React.js?
How to convert this firebase code into its latest syntax in React.js?

Time:02-11

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

I have been trying to use this code in react but I was unable to do it.

CodePudding user response:

From the docs

import { doc, onSnapshot, getFirestore } from "firebase/firestore";
db = getFirestore();

const unsub = onSnapshot(doc(db, "cities", "SF"), (doc) => {
    console.log("Current data: ", doc.data());
});
  • Related