简体   繁体   English

仅在滚动后反应原生 FlatList 渲染

[英]React native FlatList rendering only after a scroll

I have a FlatList that weirdly renders correctly only after a scroll.我有一个 FlatList,只有在滚动后才能正确呈现。 Also, upon a refresh it is rendered for a split-second and then disappears again.此外,在刷新时,它会呈现一瞬间,然后再次消失。 The FlatList itself has only 3 items. FlatList 本身只有 3 个项目。

The FlatList is defined like this in the View: FlatList 在 View 中是这样定义的:

 <View style={{flex: 1}}> <FlatList data={cartItems} renderItem={({item}) => ( <CartItemCard event={item.event_name} ticketType={item.title} ticketImage={item.image} price={item.price} /> )} keyExtractor={item => item.id} style={{ flex: 1, backgroundColor: "#49424D", paddingVertical: 60, }} contentContainerStyle={{ alignItems: 'center', paddingVertical: 10, }} /> </View>

Do you have any idea what am i doing incorrectly?你知道我做错了什么吗?

It seems to be a known issue: https://github.com/facebook/react-native/issues/13316这似乎是一个已知问题: https://github.com/facebook/react-native/issues/13316

According to the doc you should pass extraData to the FlatList so it knows to re-render:根据文档,您应该将 extraData 传递给 FlatList 以便它知道重新渲染:

By passing extraData={selectedId} to FlatList we make sure FlatList itself will re-render when the state changes.通过将 extraData={selectedId} 传递给 FlatList,我们确保 FlatList 本身会在 state 更改时重新渲染。 Without setting this prop, FlatList would not know it needs to re-render any items because it is a PureComponent and the prop comparison will not show any changes.如果不设置这个 prop,FlatList 将不知道它需要重新渲染任何项目,因为它是一个 PureComponent,并且 prop 比较不会显示任何更改。

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

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