简体   繁体   English

React Native Horizo​​ntal Flatlist - 在滚动时重新渲染整个列表?

[英]React Native Horizontal Flatlist - Re-renders entire list on scroll?

So I'm not sure if I've implemented Flatlist correctly here but every time I scroll to the right (horizontal flatlist, with one item per page), the entire list of items seems to be re-rendered (I verified this by using a console.log in the renderItem).所以我不确定我是否在这里正确实现了 Flatlist,但是每次我向右滚动(水平平面列表,每页一个项目)时,整个项目列表似乎都被重新渲染(我通过使用验证了这一点renderItem 中的 console.log)。 I have also noticed the below warning:我还注意到以下警告:

VirtualizedList: You have a large list that is slow to update - make sure your renderItem function renders components that follow React performance best practices like PureComponent, shouldComponentUpdate, etc. VirtualizedList:您有一个更新缓慢的大型列表 - 确保您的 renderItem 函数呈现遵循 React 性能最佳实践的组件,例如 PureComponent、shouldComponentUpdate 等。

The data is being populated via props from the parent component, which in turn is obtained via an API call.数据是通过父组件的 props 填充的,而父组件又是通过 API 调用获得的。 If I have < 10 items, this re-rendering doesn't seem to happen though.如果我有 < 10 个项目,那么这种重新渲染似乎不会发生。

Appreciate your help.感谢你的帮助。 Code below下面的代码

App.js应用程序.js

export default function App() {
  const [ state, setState ] = useState({
    sideMenuOpen: false,
    comingSoon: null
  })

  useEffect(() => {
    (async () => setState({comingSoon: await getComingSoon()}))()
  }, [])

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.background}>
        <Background />
      </View>
      <ScrollView>
        <View >
          <Swiper header={'Movies & TV: Coming Soon'} interval={6000} data= 
           {state.comingSoon} />
        </View>
      </ScrollView>
    </SafeAreaView>
  );
}

Swiper.js Swiper.js

class Item extends React.PureComponent {
  componentDidMount() {
    console.log('component mount', this.props.index)
  }

  componentWillUnmount() {
    console.log('component unmount', this.props.index)
  }

  componentDidUpdate(prevProps) {
    console.log('component update')
  }

  render() {
    const { content } = this.props
    return (
      <View style={styles.item}>
        <Text style={styles.contentTitle}>{content.title}</Text>
        <View style={styles.contentTextContainer}>
          <Text style={styles.contentText}>content.castList} 
          </Text>
          <Text style={styles.contentText}>{content.releaseDate}</Text>
        </View>
      </View>
    )
  }
}

const Slider = props => {

  const renderItem = ({ item }) => {
    return <Item content={item} />
  }

  return (
    <View style={styles.container}>
      <FlatList
        data={props.data}
        pagingEnabled={true}
        horizontal={true}
        renderItem={renderItem}
        keyExtractor={item => item.id}
      />
    </View>
  )
}

Output As you can see from the image, every time I scroll (example from index 10 -> 11), it unmounts the previous 10 and mounts the previous 11. This is clearly not performant and how it should work?输出正如您从图像中看到的那样,每次我滚动时(例如从索引 10 -> 11),它都会卸载前 10 并安装前 11。这显然不是高性能的,它应该如何工作? Appreciate your help.感谢你的帮助。

在此处输入图像描述

maybe a problem with your container styles around FlatList, if it's allowed to expand horizontally then it will render all items.可能是您在 FlatList 周围的容器样式存在问题,如果允许水平扩展,那么它将呈现所有项目。 it needs to be limited in width to only render the visible items.它需要限制宽度以仅呈现可见项目。

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

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