'React Native: onViewableItemsChanged: check last item is visible on screen

In my react-native app I'm using onViewableItemsChanged prop on FlatList in order to see if the last item of FlatList is visible on screen,.

To do that I want to compare changed item indexes with the number of items in FlatList, however messages.length is always zero even though it's not, I think I'm getting the length of messages array on mount which is zero.

How would you go about this problem?

const onViewRef = React.useRef((viewableItems)=> {
                //This always returns 0 so how can I check if last item is on screen
                console.log('messagesTotal:',messages.length);
                console.log('changedItems:',viewableItems.changed[0].index);
                
                setValue(viewableItems.changed[0].index);
            });
<FlatList ref={flatListRef} onViewableItemsChanged={onViewRef.current}>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source