Flatlist是否自动支持分页?

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

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来呈现产品数据,但它并没有自动处理分页。您需要手动实现分页功能。实现分页的最佳方法通常涉及以下步骤:

  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?

import { DataStore } from &#39;@aws-amplify/datastore&#39;;
import {Product} from &quot;../src/models&quot;;

const [allProducts, setAllProducts] = useState &lt;Product[]&gt;([]);

useEffect(() =&gt; {

      const getallProducts = async () =&gt; {
  
        await DataStore.query(Product, a =&gt; a.Available(&quot;eq&quot;, true)).then(setAllProducts);
          
      };
      getallProducts();
      
  
    }, []);

&lt;FlatList 
   data={allProducts}
   renderItem = {({item}) =&gt; &lt;ProductComponent info={item}  /&gt;}
   numColumns={2}
 /&gt;

答案1

得分: 2

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

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

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&lt;number&gt;(0)

const onLoadMore = async () =&gt; {
  await DataStore.query(Product, (a) =&gt; a.Available(&quot;eq&quot;, true)).then(
    () =&gt; setAllProducts([...allProducts, ...response.dataProducts]),
    { page: page, limit: 100 },
  )
  setPage(page + 1)
}

return (
  &lt;FlatList
    data={allProducts}
    renderItem = {({item}) =&gt; &lt;ProductComponent info={item} /&gt;}
    numColumns={2}
    onEndReachedThreshold={0.2} // trigger onEndReached when the end of the content is within 20% the visible length of the list.
    onEndReached={onLoadMore}
  /&gt;
)

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:

确定