I’m using FlatList
to build a TikTok-style video player, i.e. one list item covers the entire screen.
I’m fetching metadata about 10 videos at a time from a REST API (via useSWRInfinite
), then populating my FlatList
and using expo-av
’s Video
component and Mux.com for playback.
Right now onEndReached
is triggered when I get to video 10, and then the next 10 videos are loaded, etc. How can I change this so onEndReached
is triggered already on video 9?
Code
// Fine-tuning video feed experience:
const VIDEO_FEED_ITEMS_TO_FETCH_API = 10
const VIDEO_FEED_ITEMS_TO_RENDER = VIDEO_FEED_ITEMS_TO_FETCH_API - 2
const VIDEO_FEED_FLATLIST_OPTIONS = {
removeClippedSubviews: true, // views that are outside of the viewport are detached from the native view hierarchy
windowSize: 3, // measurement unit where 1 is equivalent to your viewport height
initialNumToRender: VIDEO_FEED_ITEMS_TO_RENDER, // number of items that would cover the screen for every device
maxToRenderPerBatch: VIDEO_FEED_ITEMS_TO_RENDER, // the next chunk of items rendered on every scroll
updateCellsBatchingPeriod: 100, // the time interval (ms) between each batch of items rendered
onEndReachedThreshold: 0.001 // limit where next API fetch is triggered (% of viewport height)
}
<FlatList
ref={flatList}
data={videos}
renderItem={renderItem}
initialScrollIndex={videoToScrollToIndex}
getItemLayout={getItemLayout}
onScrollToIndexFailed={handleScrollFailed}
showsVerticalScrollIndicator={false}
snapToInterval={viewAreaHeight}
snapToAlignment='start'
decelerationRate='fast'
viewabilityConfig={VIEWABILITY_CONFIG}
onViewableItemsChanged={handleViewableItemsChanged}
onEndReached={handleEndReached}
{...VIDEO_FEED_FLATLIST_OPTIONS}
/>