Skip to content

Instantly share code, notes, and snippets.

@ozcanzaferayan
Created February 10, 2024 12:14

Revisions

  1. ozcanzaferayan created this gist Feb 10, 2024.
    20 changes: 20 additions & 0 deletions useTabPress.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    import { useNavigation } from "expo-router";
    import { useEffect } from "react";
    import { FlatList } from "react-native";

    export const useTabPressScroll = <T>(ref: React.RefObject<FlatList<T>>) => {
    const navigation = useNavigation();
    useEffect(() => {
    // @ts-expect-error tabPress is not in the type
    const unsub = navigation.addListener("tabPress", (evt) => {
    if (navigation.isFocused()) {
    ref.current?.scrollToOffset({
    offset: 0,
    animated: true,
    });
    }
    });

    return unsub;
    }, [navigation]);
    };