Skip to content

Instantly share code, notes, and snippets.

@ozcanzaferayan
Created February 10, 2024 12:14
Show Gist options
  • Save ozcanzaferayan/f5115e76dc7cec8f478b97cb74c6c10e to your computer and use it in GitHub Desktop.
Save ozcanzaferayan/f5115e76dc7cec8f478b97cb74c6c10e to your computer and use it in GitHub Desktop.
useTabPress.ts
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]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment