Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to get top index of flatlist in react native

Tags:

react-native

Is there a provided method to enable us to get the index of the top item of the FlatList when we scroll it ?

like image 480
J.sparrow Avatar asked Jan 06 '18 09:01

J.sparrow


People also ask

How do you get index from FlatList in React Native?

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.

How do I use extraData in FlatList?

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.

What is the use of keyExtractor in FlatList?

It extracts the unique key name and its value and tells the FlatList component to track the items based on that value.


5 Answers

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.

like image 111
Sachin Avatar answered Oct 21 '22 00:10

Sachin


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} />    
  />
like image 39
Maksym Koshyk Avatar answered Oct 21 '22 00:10

Maksym Koshyk


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

like image 41
yangguang1029 Avatar answered Oct 21 '22 00:10

yangguang1029


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()}
    />
like image 21
Ahmad Sadiq Avatar answered Oct 20 '22 23:10

Ahmad Sadiq


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.

like image 41
bertonc96 Avatar answered Oct 20 '22 23:10

bertonc96