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
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.
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.
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.
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.
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
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>
);
}}
/>
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