Created
January 23, 2019 14:09
-
-
Save gbertoncelli/182d8c7d5449c9e8fd659de3b48bba53 to your computer and use it in GitHub Desktop.
How to get current index from swiper-flat-list or flat-list in react native.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import SwiperFlatList from "react-native-swiper-flatlist"; | |
export default class MyComponent extends React.Component { | |
// INSIDE A COMPONENT | |
constructor(props) { | |
super(props); | |
this.currentIndex = 0; | |
this._updateIndex = this._updateIndex.bind(this); | |
this.viewabilityConfig = { | |
itemVisiblePercentThreshold: 50 | |
}; | |
} | |
_updateIndex({ viewableItems }) { | |
// getting the first element visible index | |
this.currentIndex = viewableItems[0].index; | |
} | |
render() { | |
return // ... | |
<SwiperFlatList | |
loop={false} | |
index={0} | |
onViewableItemsChanged={this._updateIndex} | |
viewabilityConfig={this.viewabilityConfig} | |
> | |
{data} | |
</SwiperFlatList>; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment