Multiple call to render item
See original GitHub issueIt’s a questiob but may turn out to be a bug.
Problem: _renderItems
gets called thrice
How to replicate:
Following is the code to display a carousel in a very simple example:
import React, {PureComponent} from 'react'
import { ScrollView, Text, View, SafeAreaView,Platform, Dimensions, StyleSheet } from 'react-native';
import Carousel from 'react-native-snap-carousel';
export default class SnapDemo extends PureComponent {
constructor(props){
super(props)
this.state = {data: [{'num':1},{'num':2},{'num':3}]}
}
_renderItem (item) {
console.log(item)
return(<View key={item.item.num} style={{backgroundColor:'orange', height:100}}><Text>{item.item.num}</Text></View>)
}
render(){
return(
<SafeAreaView style={{height: 600}}>
<Carousel
data={this.state.data}
renderItem={this._renderItem}
itemWidth={Dimensions.get('window').width * 0.85}
sliderWidth={Dimensions.get('window').width}
containerCustomStyle={{flex:1}}
removeClippedSubviews={true}
keyExtractor={(item, index) => index.toString()}
slideStyle={{ flex: 1 }}/>
</SafeAreaView>
)
}
}
There are 2 problems:
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:27 (1 by maintainers)
Top Results From Across the Web
FlatList renderItem is being called multiple times
The real issue I'm facing is when I'm trying to render chat messages (fetching through API call and setting it inside the state)...
Read more >Issue #478 · meliorence/react-native-snap-carousel
Multiple call to render item #478 ... RenderItem with a FlatList gets called once only for every iterated item.
Read more >FlatList
To render multiple columns, use the numColumns prop. Using this approach instead of a flexWrap layout can prevent conflicts with the item ......
Read more >React Top-Level API
React.Fragment. The React.Fragment component lets you return multiple elements in a render() method without creating an additional DOM element:.
Read more >Layouts and Rendering in Rails
How to create layouts with multiple content sections. How to use partials to DRY ... You don't need to call to_json on the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have a similar problem. The renderItem gets triggered multiple times for the same index. It also triggers the renderItem with index 0 up to four times - no idea why this is happening
<Carousel
lockScrollWhileSnapping
ref={(c) => {
this.carousel = c;
}}
windowSize={1}
initialNumToRender={1}
maxToRenderPerBatch={1}
data={this.props.articles}
sliderWidth={this.props.screenWidth}
itemWidth={this.props.screenWidth}
itemHeight={this.props.screenHeight}
keyExtractor={(item, index) => item.article+ '-' + index}
scrollEnabled={this.isListMode() && this.state.isArticleLoaded}
onSnapToItem={this.onItemSnap}
renderItem={(params) => renderCorrectArticleDetail(params)}
/>
Even with initialNumToRender={1} and maxToRenderPerBatch={1} it still triggers renderItem 12 times. This is causing big performance issues for me. Any ideas?
I moved whole renderItem function to the new PureComponent (I use functional components, so I use React.memo)
ScrollPaneView.js:
And now renderItem is called only once for each item.