【发布时间】:2020-12-15 07:32:28
【问题描述】:
所以我有一个 FlatList,它可以接收一系列项目。当我滚动到底部时,我会将更多项目附加到该数组的末尾并显示给用户。
问题是当我们添加到我们的项目数组时,每个项目都会被渲染,有时甚至会渲染两次。
这里的代码被简化了。 /r/reactnative 无法回答这个问题。
constructor(props) {
super(props);
this.state = {itemsTest: ['A', 'A', 'A', 'A']}
}
render() {
// Key is fine, since none of my items are changing indexes. I am just adding new items.
return (
<FlatList
keyExtractor={(item,index) => index}
scroll
data={this.state.itemsTest}
renderItem={({item, index}) => <View style={{width: windowWidth}}><Text>{item}</Text></View>
onEndReached={() => this.nextItemsTest()}
onEndReachedThreshold={0.2}
</FlatList>
)
}
nextItemsTest() {
// From suggestions below, just add an element to this array.
console.log('nextItemsTest');
const x = ['A'];
// Have worked with many variations of setting state here. I don't think this is the issue.
this.setState((prevState) => ({itemsTest: [...prevState.itemsTest, ...x],}));}
这是输出。每次设置我的状态时,每个项目都会重新渲染(甚至两次)。
我只想重新渲染未更改的项目。谢谢。
【问题讨论】:
标签: javascript reactjs react-native