Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialScrollIndex not working for FlatList react native

I am experiencing an issue with the initialScrollIndex of FlatList - the argument of initialScrollIndex is simply ignored and the first item is shown.

https://snack.expo.io/Bk1mkK0zZ

like image 278
sirilicious Avatar asked Jun 14 '17 09:06

sirilicious


People also ask

How do I use scrollToIndex in FlatList React Native?

let index = 3 flatlistRef. current. scrollToIndex({ animated: true, index: index }) } useFocusEffect( useCallback(() => { scrollToIndex() }, []) ) const renderItem = ({ item, index }) => { return ( <TouchableOpacity key={item. _id} onPress={() => { handleSetSelectedCategoryId(item.

How do I refresh FlatList automatically in React Native?

React Native RefreshControl If you're rendering a list of data using the ScrollView or FlatList component, you can use RefreshControl to add the pull-to-refresh capability. When you initiate the pull-to-refresh gesture, RefreshControl triggers an onRefresh event.

How do you show data in FlatList in React Native?

Displaying a List with a React Native FlatList. The FlatList component requires two props: data and renderItem. A data prop takes an array of data that needs to be rendered, and renderItem defines a function that takes data via parameters and returns a formatted component to be displayed on the screen.

How do you give space in FlatList React Native?

You can give the item itself a width value of 45% . Also, flatlist has a property called columnWrapperStyle that you can give the value justifyContent: 'space-between . Docs: Rendered in between each item, but not at the top or bottom. Just add some margin to the style of the list Item.


2 Answers

I had the exact same problem, which is why I found your question. After a lot of testing, I found a workaround, which seems to work. Instead of using initialScrollIndex I used the function scrollToIndex once the list is rendered. Applying it to your code, it will look like

import React, { Component } from 'react';
import { FlatList, Dimensions, View, Text } from 'react-native';

const WIDTH = Dimensions.get('window').width;
const HEIGHT = Dimensions.get('window').height;

export default class App extends Component {

  render() {
    const data = [{id: 0},{id: 1},{id: 2},{id: 3},{id: 4}];

     return (
      <View onLayout={() => this.onLayout()}>
        <FlatList
          ref={el => this.list = el}
          data={data}
          renderItem={this.renderItem}
          keyExtractor={item => item.id}

          pagingEnabled={true}
          horizontal={true}
          showsHorizontalScrollIndicator={false}

          getItemLayout={this.getItemLayout}

          debug={true}
        />
      </View>
    )
  }

  onLayout() {
    this.list.scrollToIndex({index: 2})
  }

  renderItem = ({ item }) => {
    return (
      <View style={{flex: 1, backgroundColor: 'lightblue', width: WIDTH, height: HEIGHT, justifyContent: 'center', alignItems: 'center'}}>
        <Text style={{color: 'white', fontSize: 300, fontWeight: 'bold'}}>{item.id}</Text>
      </View>
    );
  };

  getItemLayout = (data, index) => (
    {length: WIDTH, offset: WIDTH * index, index}
  );
}

Hope it works for you.

By the way, putting this.list.scrollToIndex({index: 2}) in componentDidMount, does for some reason not work for me, which is why I used onLayout instead

like image 69
Spliid Avatar answered Sep 18 '22 17:09

Spliid


Using RN 0.61.5

I've solved this with setting:

  • onContentSizeChange which will handle scroll to index whenever flatlist data is changed and all items rendered ( you can handle how many items will be rendered on initial render with setting initialNumToRender if needed)

  • onScrollToIndexFailed which is necessary to use when using scrollToIndex function:

<FlatList
    ref={ref => (this.flatList = ref)}
    data={dataArray}
    style={style}
    eyExtractor={(item, index) => String(index)}
    onContentSizeChange={() => {
        if (this.flatList && this.flatList.scrollToIndex && dataArray && dataArray.length) {
            this.flatList.scrollToIndex({  index: dataArray.length - 1 });
        }
    }}
    onScrollToIndexFailed={() => {}}
    renderItem={({ item, index }) => {
        return (
            <Text>Stackoverflow Answer</Text>
        );
    }}
/>
like image 44
Darko Pranjic Avatar answered Sep 16 '22 17:09

Darko Pranjic