Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 0 additions & 79 deletions src/components/FlatList/index.android.js

This file was deleted.

43 changes: 43 additions & 0 deletions src/components/FlatList/index.android.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {useFocusEffect} from '@react-navigation/native';
import React, {ForwardedRef, forwardRef, useCallback, useContext} from 'react';
import {FlatList, FlatListProps} from 'react-native';
import {ActionListContext} from '@pages/home/ReportScreenContext';

// FlatList wrapped with the freeze component will lose its scroll state when frozen (only for Android).
// CustomFlatList saves the offset and use it for scrollToOffset() when unfrozen.
function CustomFlatList<T>(props: FlatListProps<T>, ref: ForwardedRef<FlatList>) {
const {scrollPosition, setScrollPosition} = useContext(ActionListContext);

const onScreenFocus = useCallback(() => {
if (typeof ref === 'function') {
return;
}
if (!ref?.current || !scrollPosition?.offset) {
return;
}
if (ref.current && scrollPosition.offset) {
ref.current.scrollToOffset({offset: scrollPosition.offset, animated: false});
}
}, [scrollPosition?.offset, ref]);

useFocusEffect(
useCallback(() => {
onScreenFocus();
}, [onScreenFocus]),
);

return (
<FlatList<T>
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
onScroll={(event) => props.onScroll?.(event)}
onMomentumScrollEnd={(event) => {
setScrollPosition({offset: event.nativeEvent.contentOffset.y});
}}
ref={ref}
/>
);
}

CustomFlatList.displayName = 'CustomFlatListWithRef';
export default forwardRef(CustomFlatList);
File renamed without changes.