onViewableItemsChanged not firing if items change without scroll š
See original GitHub issueIāve seen a couple of issues mentioning onViewableItemsChanged not working but thought I should open a new one because they donāt provide reproducible examples and I found them a bit unclear
Current behavior
Adding new items to the FlashList does not cause the onViewableItemsChanged
to be fired.
To Reproduce
Try tapping the button once loaded, the handler does not fire.
import { FlashList } from '@shopify/flash-list'
import React, { useState } from 'react'
import { Button, Text, ViewabilityConfig } from 'react-native'
const viewabilityConfig: ViewabilityConfig = {
itemVisiblePercentThreshold: 0,
}
function handleViewableItemsChanged() {
console.log('handleViewableItemsChanged')
}
export function ExampleFlashList() {
const [data, setData] = useState(() => Array.from({ length: 100 }, (_, i) => i))
return (
<FlashList
data={data}
inverted
ListHeaderComponent={
<Button
title="Add more items"
onPress={() => {
setData(x => [...Array.from({ length: 10 }, (_, i) => x[0] - (10 - i)), ...x])
}}
/>
}
onViewableItemsChanged={handleViewableItemsChanged}
renderItem={renderItem}
estimatedItemSize={40}
viewabilityConfig={viewabilityConfig}
keyboardDismissMode="interactive"
keyboardShouldPersistTaps="handled"
onEndReachedThreshold={0.5}
/>
)
}
function renderItem({ item }: { item: number }) {
return <Text>Item: {item}</Text>
}
Platform:
- iOS Android (untested)
Environment
1.2.2
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:8 (1 by maintainers)
Top Results From Across the Web
React Native "onViewableItemsChanged" not working while ...
I have a React Native FlatList. base on Documentation I used onViewableItemsChanged for getting the current showing item on the screenĀ ...
Read more >onViewableItemsChanged is broken if pagingEnabled is true ...
Scroll the FlatList and see the log of viewableItems , viewableItems will log all the items when scroll to the first item, which...
Read more >Understand onViewableItemsChanged in FlatList - RY 's Blog
onViewableItemsChanged is a prop in VirtualizedList and FlatList. When you scroll a FlatList, the items showing on the FlatList change.
Read more >onViewableItemsChanged: check last item is visible on screen
Hi everybody, in my react-native app I'm using onViewableItemsChanged prop on FlatList in order to see if the last item of FlatList isĀ ......
Read more >React Native scrollToIndex - Dynamic size item scroll inside ...
In this video tutorial you will learn about React Native scrollToIndex and how to scroll to an item inside a FlatList, ListView, ScrollView,Ā ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@hirbod Not OP, but my setup implements:
disableAutoLayout
estimatedItemSize
estimatedListSize
getItemType
keyExtractor
numColumns
onBlankArea
onViewableItemsChanged
overrideItemLayout
viewabilityConfig
(tried various tweaks here, and even callingflashRef.recordInteraction()
imperatively).Anyways, looking at the underlying code, itās relatively clear that the issue for me is the fact that FlashList sort of conflates āvisible indicesā with āvisible itemsā. Frequently, new items can be added, without the visible indices changing at all.
Itās very likely, therefore, that the reason I would occasionally see this fire, is because adding a new item would change the layout in such a way that visible indices changes (probably because Iām using
numColumns
).Note that I have not verified this. I just looked at the code and the fact that FlashList relies on the underlying
onVisibleIndicesChanged
from recycler. I can go into it and figure out exactly what is going on, and probably fix it, if the maintainers are interested.On my end, I already found a workaround, and that is keeping a reference to the visible indices. If something new comes in and itās within the visible range, I make sure the extra data is loaded.
Looks something like:
Not ideal, but itāll work until this is fixed.
For my particular use case all I care about is if the first visible item changed I built a hook for this inspired from @bfrickaās comment. A similar strategy could be used to fully fix FlashList