react-native-snap-carousel icon indicating copy to clipboard operation
react-native-snap-carousel copied to clipboard

Calling snapToItem results in: undefined is not an object (evaluating 'this._venueCarousel.snapToItem')

Open ragunaru opened this issue 8 years ago • 3 comments

Hi folks,

I'm trying to snap to a new item based on a condition.

My carousel setup:

      <Carousel
        ref={(venueCarousel) => { this._venueCarousel = venueCarousel; }}
        data={hits}
        renderItem={({index, item}) => (
          <MapVenueCard
             venue={item}
             index={index}
           />
        )}
        sliderWidth={width}
        itemWidth={itemWidth}
        containerCustomStyle={MapScreenStyles.VenueCardContainer}
        inactiveSlideScale={1}
        inactiveSlideOpacity={1}
        onSnapToItem={this.onSnap}
      />

I have this function that uses the snapToItem callback.

  resetCards(activeVenueCard) {
    this._venueCarousel.snapToItem(activeVenueCard, true);
  }

And then I call that function in an if statement:

    if (activeVenueCard == 0) {
      this.resetCards(activeVenueCard);
    } else {
      console.log('============ Tings are not 0');
    }

But I keep getting this error:

image

The strange thing is if I wrap the callback in a setTimeout, it works just fine!

  resetCards(activeVenueCard) {
    setTimeout(() => {
      this._venueCarousel.snapToItem(activeVenueCard, true);
    }, 500);
  }

Why could this be? Any help appreciated.

ragunaru avatar Apr 07 '18 18:04 ragunaru

@ragunaru My guess is that onSnapToItem is called during init before the reference has been created.

Try the following:

resetCards(activeVenueCard) {
  this._venueCarousel && this._venueCarousel.snapToItem(activeVenueCard, true);
}

bd-arc avatar Apr 08 '18 15:04 bd-arc

@ragunaru This should be fixed in version 3.7.0.

bd-arc avatar Apr 12 '18 19:04 bd-arc

Bin the render function in the constructor constructor(props) { super(props); this._renderItem = this._renderItem.bind(this);

jhoanborges avatar Jul 18 '19 16:07 jhoanborges