diff options
author | Ivan Chen <ivan@tagg.id> | 2021-07-01 15:33:04 -0400 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-07-01 15:33:04 -0400 |
commit | 622706f1beeb2de01554994977a66df7becd1591 (patch) | |
tree | 322cf78e35caee75c576f5de13edf55547ef111f | |
parent | 97a3f554746b3b73eb57b21e2d8b8d8b5a1a049c (diff) |
Fix crash issue on notification tap, Fix initial index issue
-rw-r--r-- | src/screens/profile/IndividualMoment.tsx | 39 |
1 files changed, 15 insertions, 24 deletions
diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index bd87f4b0..7d231312 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -46,15 +46,23 @@ const IndividualMoment: React.FC<IndividualMomentProps> = ({route}) => { const [currentVisibleMomentId, setCurrentVisibleMomentId] = useState< string | undefined >(); + const [viewableItems, setViewableItems] = useState<ViewToken[]>([]); + // https://stackoverflow.com/a/57502343 const viewabilityConfigCallback = useRef( - (info: {viewableItems: ViewToken[]; changed: ViewToken[]}) => { - const index = info.viewableItems[0].index; + (info: {viewableItems: ViewToken[]}) => { + setViewableItems(info.viewableItems); + }, + ); + + useEffect(() => { + if (viewableItems.length > 0) { + const index = viewableItems[0].index; if (index !== null && momentData.length > 0) { setCurrentVisibleMomentId(momentData[index].moment_id); } - }, - ); + } + }, [viewableItems]); useEffect(() => { const showKeyboard = () => setKeyboardVisible(true); @@ -67,10 +75,6 @@ const IndividualMoment: React.FC<IndividualMomentProps> = ({route}) => { }; }, []); - console.log('moment data length: \n ' + momentData.length); - console.log('Moment length: \n ' + moments.length); - console.log('initINDEX: \n' + initialIndex); - const ITEM_HEIGHT = SCREEN_HEIGHT; return ( <MomentContext.Provider value={{ @@ -91,25 +95,12 @@ const IndividualMoment: React.FC<IndividualMomentProps> = ({route}) => { keyExtractor={(item, _) => item.moment_id} showsVerticalScrollIndicator={false} initialScrollIndex={initialIndex} - // onViewableItemsChanged={viewabilityConfigCallback.current} - // getItemLayout={} - // snaptoIte - // snapToInterval={ITEM_HEIGHT} + onViewableItemsChanged={viewabilityConfigCallback.current} getItemLayout={(data, index) => ({ - length: ITEM_HEIGHT, - offset: ITEM_HEIGHT * index, + length: SCREEN_HEIGHT, + offset: SCREEN_HEIGHT * index, index, })} - onScrollToIndexFailed={(info) => { - console.log('this is when it fails: '); - setTimeout(() => { - console.log('trying to scroll to some index: ' + info.index); - console.log('ScrollRef: ' + scrollRef.current); - scrollRef.current?.scrollToIndex({ - index: info.index, - }); - }, 500); - }} pagingEnabled /> <TabsGradient /> |