onSnapshot()를 사용하여 실시간 업데이트를 한다.
import { collection, onSnapshot, orderBy,} from "firebase/firestore";
const [nweets, setNweets] = useState([]);
useEffect(() => {
const q = query(
collection(dbService, "nweets"),
orderBy("createdAt", "desc")
);
onSnapshot(q, (snapshot) => {
const nweetArr = snapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
setNweets(nweetArr);
});
}, []);