FlatList ScrollView Error on any State Change - Invariant Violation: Changing onViewableItemsChanged on the fly is not supported

前端 未结 7 1253
北荒
北荒 2020-12-31 10:11

onViewableItemsChanged does not seem to work when there is a state change in the app. Is this correct?

Seems like it wouldn\'t be very useful if this

相关标签:
7条回答
  • 2020-12-31 10:43

    Based on @woodpav comment. Using functional components and Hooks. Assign both viewabilityConfig and onViewableItemsChanged to refs and use those. Something like below:

      const onViewRef = React.useRef((viewableItems)=> {
          console.log(viewableItems)
          // Use viewable items in state or as intended
      })
      const viewConfigRef = React.useRef({ viewAreaCoveragePercentThreshold: 50 })
    
    
    <FlatList
          horizontal={true}
          onViewableItemsChanged={onViewRef.current}
          data={Object.keys(cards)}
          keyExtractor={(_, index) => index.toString()}
          viewabilityConfig={viewConfigRef.current}
          renderItem={({ item, index }) => { ... }}
    />
    
    0 讨论(0)
提交回复
热议问题