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());
});