Is there a provided method to enable us to get the index of the top item of the FlatList when we scroll it ?
To get the index of the currently visible item in a React Native flat list, we can set the onViewableItemsChange prop to a function that gets the current visible items. to define the onViewableItemsChanged function that gets the viewableItems property. viewableItems has the items that are current visible on the screen.
flatlist-simple By passing extraData={selectedId} to FlatList we make sure FlatList itself will re-render when the state changes. Without setting this prop, FlatList would not know it needs to re-render any items because it is a PureComponent and the prop comparison will not show any changes.
It extracts the unique key name and its value and tells the FlatList component to track the items based on that value.
You can access the index of each row in this way:
<FlatList data={this.state.dataList}
...
renderItem={({item, index}) => this.renderRow(item, index)}
...
console.log("current index is " + index)
/>
this will give you the index of the top row.
In order to get an index of FlatList item - spread the itemData inside renderItem.
<FlatList
data={someData}
keyExtractor={(item, index) => index.toString()}
renderItem={ ( {item, index} ) => // use {item, index} instead of itemData
<MyComponent index={index} />
/>
there is no method to get it directly.
but you can compute it by scrollview offset, if you know the layout of every item.
implement onScroll on flatlist, like
<FlatList onScroll={(e)=>{
let offset = e.nativeEvent.contentOffset.y;
let index = parseInt(offset / height); // your cell height
console.log("now index is " + index)
}}>
ps: don't forget to subtract the header layout if contains
Here is the right way to get index of items of flat list:
<FlatList
horizontal
data={this.state.data}
renderItem={({ item: rowData,index }) => {
return (
<Card
containerStyle={{flex:1,marginBottom:30,backgroundColor:'rgba(255,255,255,0.7)',}}>
<Text style={{ marginBottom: 10 ,fontSize:30}}>
{'Comments:'}
</Text>
<Text style={{width:290}}>
{rowData.comment}
</Text>
<Text style={styles.text }>
{'\nIndex:'+index}
</Text>
</Card>
);
}}
keyExtractor={(item,index) => index.toString()}
/>
Actually you have to use the prop onViewableItemsChanged and get from the data returned by the callback the index of the first element displayed in the list. Remember to set also the prop viewabilityConfig so the component can determine where you consider an object displayed or not.
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>;
}
}
instead of swiper-flat-list you can use the FlatList.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With