【问题标题】:How i can limit the items in the FlatList and add load more?我如何限制 FlatList 中的项目并增加负载?
【发布时间】:2018-05-05 19:00:21
【问题描述】:

我的技能是基本的,我是 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 个?
  • 是的,第二个选项
  • 你最初得到多少数据长度,调用下一组项目的api有什么限制吗?
  • 我找到了这个库,但我不知道如何使用它github.com/BBuzzArt/react-native-infinite

标签: reactjs react-native


【解决方案1】:

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

在构造函数中将当前page编号设置为0

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}
    />
    ...
  );
}

希望这会有所帮助!

【讨论】:

  • 我测试了它,但我得到这个错误 TypeError: undefined is not an object evaluation '_this.state.records[i]'
  • 我的错,是dataPosts。我会更新答案。
  • 我已经更新了答案,请查看addRecords
  • 现在我得到这个错误:TypeError: Array.from 需要一个类似数组的对象,不为空或未定义
  • 我不确定您从后端获得了什么数据,请确认this.state.dataPosts 是一个数组。
【解决方案2】:

您可以在数据源中获取 jsondata 时添加 slice(start,end) 方法。这个技巧可能会解决你的问题。

dataPosts: responseJson.slice(0,10) 将此行替换为您的。

【讨论】:

    猜你喜欢
    • 2017-04-02
    • 2018-09-11
    • 1970-01-01
    • 2011-11-26
    • 2015-11-24
    • 2015-10-11
    • 1970-01-01
    • 2021-05-22
    • 2011-04-06
    相关资源
    最近更新 更多