Skip to content

Instantly share code, notes, and snippets.

@HighSoftWare96
Created January 23, 2019 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HighSoftWare96/182d8c7d5449c9e8fd659de3b48bba53 to your computer and use it in GitHub Desktop.
Save HighSoftWare96/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.
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