I’m building an image gallery app in React Native using a FlatList component. I want users to be able to enter an image ID and have the FlatList automatically scroll to that specific image. My code works perfectly when the FlatList is set to horizontal orientation, but scrolling fails when I switch to vertical orientation.
Here’s the relevant part of my code:
<FlatList
ref={flatListRef}
data={images}
getItemLayout={getItemLayout} // Assuming getItemLayout is correctly implemented
onScrollToIndexFailed={onScrollToIndexFailed} // Retry scrolling after a brief delay
renderItem={ /* ... */ }
/>
// ... in handleScrollToIndex function
flatListRef.current?.scrollToIndex({animated: true, index});
and a link to full code:
text
I’ve implemented getItemLayout to provide the layout details for each item and added onScrollToIndexFailed to retry the scrolling in case of initial failure. Am I missing something specific for vertical scrolling? How can I make the scrollToIndex function work reliably in vertical mode?
Talal Sharaa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.