简体   繁体   English

React-Native重新渲染平面列表

[英]React-Native re-render Flatlist

I'm having trouble keeping the data in my Flatlist after coming back from another page. 从另一个页面返回后,我无法将数据保存在我的Flatlist中。 My scenario is as follows: 我的情况如下:

  1. User goes to homepage and scrolls through 20 items 用户转到主页并滚动浏览20个项目
  2. User clicks their profile tab changing page using react-native-router-flux 用户使用react-native-router-flux单击其个人资料选项卡更改页面
  3. User clicks the home tab taking them back to the list however the list re-renders and starts from the top. 用户单击主页选项卡将其带回到列表,但是列表重新呈现并从顶部开始。

How can I stop this re-rendering and fetching the same data again? 如何停止重新渲染并再次获取相同的数据?

componentDidMount() {
    this.makeRemoteRequest();
}

makeRemoteReuest gets my data from firebase in batches of 5 and sets data: [] makeRemoteReuest以5为批次从firebase中获取我的数据并设置数据:[]

data: [...this.state.data, ...results]

I've tried using the below but not sure if this is correct, when i navigate away and back the data re-renders. 我尝试使用下面的方法,但是当我导航离开并返回数据时,不确定是否正确。 I want to keep the data so the page will be exactly the same as when it was left. 我想保留数据,以便页面与离开时的页面完全相同。

shouldComponentUpdate(nextProps, nextState) {
    if (JSON.stringify(this.state.data) !== JSON.stringify(nextState.data)) {
      return true;
    }
    return false;
}

My flatlist: 我的清单:

<View>
        <FlatList
          scrollsToTop={false}
          ref={(ref) => { this.flatListRef = ref; }}
          showsHorizontalScrollIndicator={false}
          onScroll={this.handleScroll}
          data={this.state.data}
          keyExtractor={item => item.key}
          ListFooterComponent={this.renderFooter()}
          onRefresh={this.handleRefresh}
          refreshing={this.state.newRefresh}
          onEndReached={this.handleEndRefresh}
          onEndReachedThreshold={0.05}
          getItemLayout={this.getItemLayout}
          renderItem={this.renderItem}
        />
        {this.state.refreshAvailable ? this.renderRefreshButton() : null}
      </View>

Thanks for any help! 谢谢你的帮助!

Coded long back for the dumb project, maybe this can help you 很久以前就为愚蠢的项目编码,也许这可以帮助您

The View: used onLayout Prop for getting the y-axis 视图:使用onLayout属性获取y轴

   <ScrollView
   ref={(ref) => this.scrollTo = ref}
   contentContainerStyle={{margin:5,}}
   >
   <Card onLayout={(event) => this._findHeight(event.nativeEvent.layout, 'personal')}>
    <Personal review={true}/>
   </Card>
   </ScrollView>

The Function: stored the y-axis; 功能:存储y轴; here i have used realm db 在这里我已经使用了领域数据库

    _findHeight = (e, name) => {
      const {x, y, width, height} = e;
      this.realm.write(() => {
         this.realm.create('yLocation',{names:name,yaxis:y}) : 
      });
    }

The AutoScroll Method : here i have used scrollTo method from ScrollView you can use any method using their ref AutoScroll方法 :在这里,我使用了ScrollView中的scrollTo方法,可以使用其引用使用任何方法

_scrollTo = (y) => {
  this.scrollTo.scrollTo({x:0,y:y,animated:true});
}

Note : Call _scrollTo method in componentDidMount 注意:在componentDidMount中调用_scrollTo方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM