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

前端 未结 7 1269
北荒
北荒 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:32

    You must pass in a function to onViewableItemsChanged that is bound in the constructor of the component and you must set viewabilityConfig as a constant outside of the Flatlist.

    Example:

    class YourComponent extends Component {
    
        constructor() {
            super()
            this.onViewableItemsChanged.bind(this)
        }
    
        onViewableItemsChanged({viewableItems, changed}) {
            console.log('viewableItems', viewableItems)
            console.log('changed', changed)
        }
    
        viewabilityConfig = {viewAreaCoveragePercentThreshold: 50}
    
        render() {
            return(
              
                    
                        Dogs and Cats
                     }
              />
            )
        }
    }
    

提交回复
热议问题