我使用react钩子,firebase来构建react本机应用程序。
我很想在FlatList中使用Hooks。
我正在尝试使用Firestore创建分页。但是,我不确定如何使用onEndReached
useEffect
这是我尝试过的:
const [isFetching, setIsFetching] = useState(false)
useEffect(() => {
if(isFetching == false){
_initFirestoreQuery()
}
}, [])
_initFirestoreQuery = async()=>{
const limit = 6
const initialQuery = firebase.firestore().collection('xxxx').orderBy('displayName', 'desc').limit(limit)
const documentSnapshots = await initialQuery.get()
const documentData = documentSnapshots.docs.map(document => document.data());
const lastVisible = documentData[documentData.length - 1].displayName;
setLast(lastVisible)
setData(documentData)
setIsFetching(true)
}
_moreFirestoreQuery = async () =>{
if(isFetching == true){
const limit = 6
const additionalQuery = firebase.firestore().collection('xxxx').orderBy('displayName', 'desc').startAfter(last).limit(limit)
const documentSnapshots = await additionalQuery.get();
const documentData = documentSnapshots.docs.map(document => document.data());
const lastVisible = documentData[documentData.length - 1].displayName;
setLast(lastVisible)
setData(data =>[...data,...documentData])
}
}
<FlatList
onEndReached ={_moreFirestoreQuery}
...
>
如果不可能的话,我有做任何事情的想法,并且有其他替代方法