Calling snapToItem results in: undefined is not an object (evaluating 'this._venueCarousel.snapToItem')
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:

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 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);
}
@ragunaru This should be fixed in version 3.7.0.
Bin the render function in the constructor
constructor(props) {
super(props);
this._renderItem = this._renderItem.bind(this);