我如何限制FlatList中的项目并增加更多载荷?

机器人

我的技能很基本,而且我是React native的新手,我想做的就是将帖子限制在12个,并且当用户滚动时会自动加载更多帖子。

我的代码:

export default class Posts extends Component {
constructor(props) {
super(props);
this.state = {
  isLoading: true,};}

componentDidMount() {
   return fetch(ConfigApp.URL+'json/data_posts.php')
     .then((response) => response.json())
     .then((responseJson) => {
       this.setState({
         isLoading: false,
         dataPosts: responseJson
       }, function() {
       });
     })
     .catch((error) => {
     });}

render() {
return (
    <FlatList
      data={ this.state.dataPosts }
      numColumns={2}
      renderItem={({item}) => 
            <TouchableOpacity activeOpacity={1} style={{flex: 1}}>
            <View style={{margin: 5, marginLeft: 4}}>
            <ImageBackground source={{uri: ConfigApp.IMAGESFOLDER+item.post_image}}>
                <LinearGradient colors={['rgba(0,0,0,0.3)', 'rgba(0,0,0,0.8)']}>
                        <Text numberOfLines={2}>{item.post_title}</Text>
                </LinearGradient>
            </ImageBackground>
            </View>
            </TouchableOpacity>
}
    keyExtractor={(item, index) => index}

    />
);}}
普拉松

如果您的要求是将已拉出的数据中的现有列表添加到12个数据块中,那么您可以考虑遵循使用onEndReachedonEndThreshold处理滚动的策略,并一次添加12条记录。

将当前page数字设置0in构造函数

constructor(props){
  super(props);
  this.state = {
    ... ,
    page: 0,
    posts: []
  }
}

在内部,componentDidMount您需要从服务器提取所有数据并将其存储在本地状态(当前正在执行),然后调用该函数,该函数将读取前12条记录。

componentDidMount() {
   return fetch(ConfigApp.URL+'json/data_posts.php')
   .then((response) => response.json())
   .then((responseJson) => {
     this.setState({
       isLoading: false,
       page: 0,
       dataPosts: responseJson
     }, function() {
       // call the function to pull initial 12 records
       this.addRecords(0);
     });
   })
   .catch((error) => {
   });
}

现在添加将添加记录的功能 this.state.dataPosts

addRecords = (page) => {
  // assuming this.state.dataPosts hold all the records
  const newRecords = []
  for(var i = page * 12, il = i + 12; i < il && i < 
    this.state.dataPosts.length; i++){
    newRecords.push(this.state.dataPosts[i]);
  }
  this.setState({
    posts: [...this.state.posts, ...newRecords]
  });
}

现在添加滚动处理程序

onScrollHandler = () => {
  this.setState({
    page: this.state.page + 1
  }, () => {
    this.addRecords(this.state.page);
  });
}

渲染功能

render() {
  return(
    ...
    <FlatList
       ...
       data={this.state.posts}
       renderItem={({item}) => ... }
       keyExtractor={(item, index) => index}
       onEndReached={this.onScrollHandler}
       onEndThreshold={0}
    />
    ...
  );
}

希望这会有所帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

FlatList 的推荐项目限制

如何从 FlatList 中的存储中删除项目?

我如何在React-native和Redux中的FlatList中渲染2个项目

为什么我不能在 Flatlist 中显示我的项目?

如何增加textares的限制?

如何增加BottomBar中项目之间的距离?

如何增加元组中的项目

我无法通过useState挂钩从FlatList中删除项目

如何在我的ASP.NET Core Web应用程序中增加JSON反序列化MaxDepth限制

如何限制我在codeigniter项目的根目录中创建的文件夹?

如何在React Native中更新FlatList中的单个项目?

如何在React Native中从FlatList中删除项目/索引?

我如何自定义或向FlatList,ListItem添加更多字段,除了“ title”和“ subtitle”

在Firebase中创建新项目时提示错误以增加项目限制

如何增加boxfuse中打开文件的用户限制?

如何在Chrome中增加Cookie的存储限制(4096)

java-grpc:如何增加ManagedChannel中的消息大小限制?

在iOS中,如何增加每个主机的HTTP连接限制?

如何增加Docker容器中Corda节点的内存限制

如何增加Google Cloud Run中的内存限制?

如何在Bootstrap 3中增加进度栏的限制

请告诉我如何增加字数限制而不是字符数限制

如何在 FlatList 中的项目之间切换?

如何减少水平Flatlist中项目之间的空间

如何在 React Native 中保存 FlatList 中的项目?

如何为Flatlist中的单个项目制作动画

ReactNative,如何在ScrollView中获得FlatList项目的位置

在 FlatList 中添加“查看更多”按钮?

如何增加onLongPress的时间限制?