英文:
Does flatlist automatically support pagination?
问题
在我的React Native应用程序中,我使用'FlatList'来呈现从后端查询的产品,如下所示。我想知道这是否自动具有分页功能,还是我需要手动实现分页,如果需要手动实现,最佳方法是什么?
import { DataStore } from '@aws-amplify/datastore';
import { Product } from "../src/models";
const [allProducts, setAllProducts] = useState<Product[]>([]);
useEffect(() => {
const getAllProducts = async () => {
await DataStore.query(Product, a => a.Available("eq", true)).then(setAllProducts);
};
getAllProducts();
}, []);
<FlatList
data={allProducts}
renderItem={({ item }) => <ProductComponent info={item} />}
numColumns={2}
/>
这段代码中,您使用了FlatList来呈现产品数据,但它并没有自动处理分页。您需要手动实现分页功能。实现分页的最佳方法通常涉及以下步骤:
- 跟踪当前页面和每页的项目数量。
- 当用户滚动到列表底部时,加载更多项目。
- 更新数据源以包括新加载的项目。
您可以使用FlatList的onEndReached
属性来检测用户是否滚动到列表底部,然后触发加载更多项目的逻辑。
英文:
In my react native application I use a 'Flatlist' to render my products which are queried from backend as shown below. I want to know does this automatically has pagination or do I have to implement pagination manually, if so what is the best way to do it?
import { DataStore } from '@aws-amplify/datastore';
import {Product} from "../src/models";
const [allProducts, setAllProducts] = useState <Product[]>([]);
useEffect(() => {
const getallProducts = async () => {
await DataStore.query(Product, a => a.Available("eq", true)).then(setAllProducts);
};
getallProducts();
}, []);
<FlatList
data={allProducts}
renderItem = {({item}) => <ProductComponent info={item} />}
numColumns={2}
/>
答案1
得分: 2
Idea: 滚动到FlatList的底部,调用获取数据的函数并将响应映射到当前数据。
您可以使用FlatList的属性来实现这一点:
- onEndReachedThreshold:列表底部在可见列表长度单位内离结束多远。
- onEndReached:当滚动位置接近onEndReachedThreshold时的回调。
AWS Amplify DataStore文档:
https://docs.amplify.aws/lib/datastore/data-access/q/platform/js/#pagination
示例:
const [page, setPage] = React.useState<number>(0)
const onLoadMore = async () => {
await DataStore.query(Product, (a) => a.Available("eq", true), { page: page, limit: 100 }).then(
(response) => setAllProducts([...allProducts, ...response.dataProducts])
)
setPage(page + 1)
}
return (
<FlatList
data={allProducts}
renderItem={({ item }) => <ProductComponent info={item} />}
numColumns={2}
onEndReachedThreshold={0.2} // 当内容末尾在列表可见长度的20%内时触发onEndReached。
onEndReached={onLoadMore}
/>
)
请注意,我已经根据您提供的代码部分进行了翻译,如果您需要更多帮助或有其他问题,请随时提出。
英文:
Idea: Scroll to FlatList's bottom, call function get data and map response to current data.
You can do that with FlatList's props:
- onEndReachedThreshold: How far from the end (in units of visible length of the list) the bottom edge of the list.
- onEndReached: callback when scroll position gets within onEndReachedThreshold.
AWS-amplify DataStore doc:
https://docs.amplify.aws/lib/datastore/data-access/q/platform/js/#pagination
Example:
const [page, setPage] = React.useState<number>(0)
const onLoadMore = async () => {
await DataStore.query(Product, (a) => a.Available("eq", true)).then(
() => setAllProducts([...allProducts, ...response.dataProducts]),
{ page: page, limit: 100 },
)
setPage(page + 1)
}
return (
<FlatList
data={allProducts}
renderItem = {({item}) => <ProductComponent info={item} />}
numColumns={2}
onEndReachedThreshold={0.2} // trigger onEndReached when the end of the content is within 20% the visible length of the list.
onEndReached={onLoadMore}
/>
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论