简体   繁体   English

在同一屏幕上处理多个平面列表

[英]Dealing With Multiple Flatlists on the same Screen

Please help my deal with unnecessary re-renders when having two flatlists on the same screen当在同一个屏幕上有两个平面列表时,请帮助我处理不必要的重新渲染

My screen requires two flatlists-我的屏幕需要两个平面列表-

  1. For HOSTS对于主机
  2. For QUEUE对于队列

When the component mounts, I get data from the api call like this-当组件安装时,我从 api 调用中获取数据,如下所示 -

{
  "hosts": [{"id":"1", "name":"kyouma"},...],
  "queue": [{"id":"99", "name":"eren"},...]
}

Now what I do is I store my hosts and queue separately in my redux store like this-现在我要做的是将我的主机队列分别存储在我的 redux 存储中,如下所示 -

this.props.dispatch({
        type: GET_ROOM_HOSTS,
        payload: info['hosts']
      })

this.props.dispatch({
        type: GET_ROOM_QUEUE,
        payload: info['queue']
      })

where info is the object received from the api call as shown above.其中info是从 api 调用收到的 object,如上所示。 Now I mapStateToProps these two from the redux store to the default screen component such that-现在我将这两个从redux存储映射到默认屏幕组件,这样 -

  1. this.props.roomQueue is for queue and this.props.roomQueue用于队列
  2. this.props.roomHosts is for hosts this.props.roomHosts用于主机

My FlatList's are like this-的 FlatList是这样的——

<FlatList
   data={this.props.roomQueue}
   horizontal={true}
   keyExtractor = {item => item.id}
   renderItem({item} => {
    return(
        <customComponent (with suitable props) ..../>
        )
    })
/>
<FlatList
   data={this.props.roomHosts}
   numColumns={3}
   keyExtractor = {item => item.id}
   renderItem({item} => {
    return(
        <customComponent (with suitable props) ..../>
        )
    })
/>

PLEASE NOTE that both the FlatList's are present in the same Component (React.Component) of the screen and are displayed at different parts of the screen(queue at bottom of the screen and hosts at the top of the screen).请注意,两个FlatList都存在于屏幕的同一组件 (React.Component) 中,并显示在屏幕的不同部分(屏幕底部的队列和屏幕顶部的主机)。 Also queue and hosts are independent of each other.队列主机也是相互独立的。 Screen looks like this屏幕看起来像这样在此处输入图像描述

My problem is that even if there is a change in this.props.roomQueue , the FlatList having its data={this.props.roomHosts} get's re-rendered.我的问题是,即使this.props.roomQueue发生变化,具有data={this.props.roomHosts}FlatList也会重新渲染。

How do i prevent this re-render to ensure that only if the FlatList's corresponding data changes, then only will it re-render, otherwise it won't.我如何防止这种重新渲染以确保只有当FlatList 的相应数据发生变化时,才会重新渲染,否则不会。 Do I have to change the way I store queue and hosts ?我是否必须更改存储队列主机的方式? or is there something else?还是有别的东西?

You can do this with using only one flatlist.您可以只使用一个平面列表来做到这一点。 Merge your both array's into one and show results from one list.. you can spare them in ui with a type.将您的两个数组合并为一个并显示一个列表中的结果..您可以在 ui 中使用类型将它们保留。

This is a genuine procedure of what developers do, cz rendering 2 list in same page and same direction is accually no mean.这是一个真正的开发者做的程序,cz在同一个页面和同一个方向渲染2个列表实际上是没有意思的。 Your query is valid.您的查询有效。

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

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