react-native - 在 React Native 中重新渲染 FlatList 时,未调用 Flatlist onVIewableItemsChanged 属性
问题描述
我正在使用 flatlist ,其中有一个名为 onViewableItemChanged 的道具,每次重新渲染 flatlist 时都需要调用它。但是,即使由于 flatlist 更新而导致 flatlist 数据发生变化,但仅当在可视区域中添加新行(即最初 6 行,然后添加一个新行,然后将被调用)或用户交互(即滚动列表)时才会调用 onViewableItemsChanged 道具。
但如果第 1 行有 20 行和数据更新,则不会调用它。
有什么办法可以解决这个问题。?提前谢谢。
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
cardData: [{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'}]
}
this.handleViewableItemsChanged = this.handleViewableItemsChanged.bind(this)
this.viewabilityConfig = {viewAreaCoveragePercentThreshold: 50}
}
handleViewableItemsChanged(info) {
console.log(info)
console.log("potato")
}
render() {
return (
<View style={styles.container}>
<Button
title="Some State Change Button"
onPress={()=>
this.setState({
cardData: [{name: 'Clean Up the FOREST'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},{name:'true'},
{name: 'Clean Up the Oceans'}, {name: 'three'},
{name: 'Clean Up the Oceans'}, {name: 'three'},
{name: 'Clean Up the Oceans'}, {name: 'three'},
{name: 'Clean Up the Oceans'}, {name: 'three'},
{name: 'Clean Up the Oceans'}, {name: 'three'},
{name: 'Clean Up the Oceans'}, {name: 'three'}
]})
}
/>
<FlatList
ref='FlatList'
data={this.state.cardData}
extraData={this.state.cardData}
horizontal={false}
pagingEnabled={true}
showsHorizontalScrollIndicator={false}
onViewableItemsChanged={this.handleViewableItemsChanged}
viewabilityConfig={this.viewabilityConfig}
renderItem={({item}) =>
<View style={{width: width, borderColor: 'white', borderWidth: 20,backgroundColor:'yellow'}}>
<Text>{item.name}</Text>
<Text>Hello World</Text>
</View>
}
/>
</View>
);
}
}
解决方案
当您更改数据或重新呈现平面列表时会发生这种情况。
只需使用参考:
const handleViewableItemsChanged = useRef(({ viewableItems }) => { //todo });
.
.
.
<FlatList
...
onViewableItemsChanged={handleViewableItemsChanged.current}
...
/>
推荐阅读
- java - 打印使用优先级队列排序的 hashmap 实例
- python - 如何在Python中编写位长不是8的倍数的二进制文件?
- laravel - 如何从 Laravel 请求对象中选择特定项目而不转换为数组
- java - 将 2D 锯齿状数组转换为正确的数组 Java
- azureservicebus - 服务总线关闭时的预定消息传递
- node.js - Discord 机器人令牌自行重置
- javascript - 遍历 Javascript 数组以计算保龄球得分
- javascript - Lodash isEqualWith 定制器不检查字段
- python - 如何修复“QWizardPage 中的错误布局”
- javascript - javaScript .bind() 不会返回一个新函数,该函数在调用时将具有等于我传递的 arg 的 'this'