question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple call to render item

See original GitHub issue

It’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:

  1. RenderItems is getting called thrice Image

  2. I see a virtualized key warning message despite official docs mentioning that explicit key isn’t required to be specified Image

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:27 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
baetejacommented, Apr 2, 2019

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?

2reactions
Cactiwcommented, May 3, 2021

I moved whole renderItem function to the new PureComponent (I use functional components, so I use React.memo)

import React from "react";

export default React.memo(() =>
    const daysToRender = [0, 1, 2, 3]
    return(
            <Carousel data={daysToRender} renderItem={
                     (props) => <ScrollPaneView item={props.item} index={props.index}/>
))

ScrollPaneView.js:

export default React.memo(({item, index} => {
    console.log("Rendering", index)
    return (
        <View><Text>Rendered</Text></View>
    )
}

And now renderItem is called only once for each item.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found