Flatlist是否自动支持分页?

huangapple go评论82阅读模式
英文:

Does flatlist automatically support pagination?

问题

在我的React Native应用程序中,我使用'FlatList'来呈现从后端查询的产品,如下所示。我想知道这是否自动具有分页功能,还是我需要手动实现分页,如果需要手动实现,最佳方法是什么?

  1. import { DataStore } from '@aws-amplify/datastore';
  2. import { Product } from "../src/models";
  3. const [allProducts, setAllProducts] = useState<Product[]>([]);
  4. useEffect(() => {
  5. const getAllProducts = async () => {
  6. await DataStore.query(Product, a => a.Available("eq", true)).then(setAllProducts);
  7. };
  8. getAllProducts();
  9. }, []);
  10. <FlatList
  11. data={allProducts}
  12. renderItem={({ item }) => <ProductComponent info={item} />}
  13. numColumns={2}
  14. />

这段代码中,您使用了FlatList来呈现产品数据,但它并没有自动处理分页。您需要手动实现分页功能。实现分页的最佳方法通常涉及以下步骤:

  1. 跟踪当前页面和每页的项目数量。
  2. 当用户滚动到列表底部时,加载更多项目。
  3. 更新数据源以包括新加载的项目。

您可以使用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?

  1. import { DataStore } from &#39;@aws-amplify/datastore&#39;;
  2. import {Product} from &quot;../src/models&quot;;
  3. const [allProducts, setAllProducts] = useState &lt;Product[]&gt;([]);
  4. useEffect(() =&gt; {
  5. const getallProducts = async () =&gt; {
  6. await DataStore.query(Product, a =&gt; a.Available(&quot;eq&quot;, true)).then(setAllProducts);
  7. };
  8. getallProducts();
  9. }, []);
  10. &lt;FlatList
  11. data={allProducts}
  12. renderItem = {({item}) =&gt; &lt;ProductComponent info={item} /&gt;}
  13. numColumns={2}
  14. /&gt;

答案1

得分: 2

Idea: 滚动到FlatList的底部,调用获取数据的函数并将响应映射到当前数据。

您可以使用FlatList的属性来实现这一点:

AWS Amplify DataStore文档:
https://docs.amplify.aws/lib/datastore/data-access/q/platform/js/#pagination

示例:

  1. const [page, setPage] = React.useState<number>(0)
  2. const onLoadMore = async () => {
  3. await DataStore.query(Product, (a) => a.Available("eq", true), { page: page, limit: 100 }).then(
  4. (response) => setAllProducts([...allProducts, ...response.dataProducts])
  5. )
  6. setPage(page + 1)
  7. }
  8. return (
  9. <FlatList
  10. data={allProducts}
  11. renderItem={({ item }) => <ProductComponent info={item} />}
  12. numColumns={2}
  13. onEndReachedThreshold={0.2} // 当内容末尾在列表可见长度的20%内时触发onEndReached。
  14. onEndReached={onLoadMore}
  15. />
  16. )

请注意,我已经根据您提供的代码部分进行了翻译,如果您需要更多帮助或有其他问题,请随时提出。

英文:

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:

  1. const [page, setPage] = React.useState&lt;number&gt;(0)
  2. const onLoadMore = async () =&gt; {
  3. await DataStore.query(Product, (a) =&gt; a.Available(&quot;eq&quot;, true)).then(
  4. () =&gt; setAllProducts([...allProducts, ...response.dataProducts]),
  5. { page: page, limit: 100 },
  6. )
  7. setPage(page + 1)
  8. }
  9. return (
  10. &lt;FlatList
  11. data={allProducts}
  12. renderItem = {({item}) =&gt; &lt;ProductComponent info={item} /&gt;}
  13. numColumns={2}
  14. onEndReachedThreshold={0.2} // trigger onEndReached when the end of the content is within 20% the visible length of the list.
  15. onEndReached={onLoadMore}
  16. /&gt;
  17. )

huangapple
  • 本文由 发表于 2023年2月10日 12:40:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定