2 回答

TA貢獻1866條經驗 獲得超5個贊
取決于您可能想要切換到的功能.get
但由于這應該像 @Nooruddin 建議的那樣是異步的,因此您需要有一個.then才能使用集合的快照
嘗試:
useEffect(() => {
const getTweets = async() => {
let newArray = []
await firebase.firestore().collection('tweet').orderBy('date', 'desc').get()
.then(((snapshot) => {
snapshot.forEach((singleTweet) => {
newArray.push(singleTweet.data())
})
}))
showLoader()
setTweets(newArray)
if (newArray) {
hideLoader()
}
}
getTweets();
}, [])

TA貢獻1789條經驗 獲得超8個贊
嘗試await像這樣放置異步
useEffect(() => {
...
const getTweets = async () => {
await firebase.firestore().collection('tweet').orderBy('date',
'desc').onSnapshot((singleTweet) => {
....
})
...
}
...
}, [])
添加回答
舉報