我的技能是基本的,我是React native的新手,我想做的是将帖子限制在12个小时内,当用户滚动自动加载更多帖子时.

My Code:

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号设置为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}
    />
    ...
  );
}

希望这会有帮助!

React-native相关问答推荐

无法读取 @react-native-async-storage/async-storage 的未定义属性setItem

如何在堆栈的任何屏幕的后退按钮按下时始终导航到堆栈的初始路径?

TypeError:未定义不是对象(判断'theme.spacing [radius]')

如何在react-native 中设置视图的自动高度

React Native Child Parent 通信

react native 中 componentDidMount 和 componentDidUpdate 有什么区别

用于部署后设置的 React 配置文件

React Native - ScrollView 中的 ListView 分页?

运行react应用程序时出错

使用 react-native 单击时如何更改图像和文本 colored颜色 ?

在 UIManager 中找不到RNCSafeAreaView

在 react-native 和 firebase 3.1 中登录 Facebook

react-native async 函数返回 promise 但不返回我的 json 数据?

使用带有样式组件的动画

在 React-native 上加载图像时出错:Unexpected character

JSON 解析错误:无法识别的令牌'<'

如何升级 react-native gradle 版本

如何在 React Native 中创建拖放操作?

将现有的 React Native 项目转换为 Expo

/bin/sh: adb: 找不到命令