Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more reliable event handling between pan gesture and FlatList #469

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/components/DraggableFlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function DraggableFlatListInner<T>(props: DraggableFlatListProps<T>) {
keyToIndexRef,
propsRef,
animationConfigRef,
panRef,
} = useRefs<T>();
const {
activeCellOffset,
Expand Down Expand Up @@ -264,9 +265,11 @@ function DraggableFlatListInner<T>(props: DraggableFlatListProps<T>) {
const gestureDisabled = useSharedValue(false);

const panGesture = Gesture.Pan()
.withRef(panRef)
.onBegin((evt) => {
gestureDisabled.value = disabled.value;
if (gestureDisabled.value) return;
runOnJS(reset)();
panGestureState.value = evt.state;
})
.onUpdate((evt) => {
Expand Down Expand Up @@ -386,7 +389,9 @@ function DraggableFlatListInner<T>(props: DraggableFlatListProps<T>) {
keyExtractor={keyExtractor}
onScroll={scrollHandler}
scrollEventThrottle={16}
simultaneousHandlers={props.simultaneousHandlers}
simultaneousHandlers={([panRef] as React.Ref<any>[]).concat(
props.simultaneousHandlers ?? []
)}
removeClippedSubviews={false}
/>
{!!props.onScrollOffsetChange && (
Expand Down
5 changes: 4 additions & 1 deletion src/context/refContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useContext } from "react";
import { useMemo, useRef } from "react";
import { FlatList } from "react-native-gesture-handler";
import { FlatList, GestureType } from "react-native-gesture-handler";
import Animated, { WithSpringConfig } from "react-native-reanimated";
import { DEFAULT_PROPS } from "../constants";
import { useProps } from "./propsContext";
Expand All @@ -14,6 +14,7 @@ type RefContextValue<T> = {
containerRef: React.RefObject<Animated.View>;
flatlistRef: React.RefObject<FlatList<T>> | React.ForwardedRef<FlatList<T>>;
scrollViewRef: React.RefObject<Animated.ScrollView>;
panRef: React.MutableRefObject<GestureType | undefined>;
};
const RefContext = React.createContext<RefContextValue<any> | undefined>(
undefined
Expand Down Expand Up @@ -63,6 +64,7 @@ function useSetupRefs<T>({
const flatlistRefInternal = useRef<FlatList<T>>(null);
const flatlistRef = flatListRefProp || flatlistRefInternal;
const scrollViewRef = useRef<Animated.ScrollView>(null);
const panRef = useRef<GestureType | undefined>(undefined);

// useEffect(() => {
// // This is a workaround for the fact that RN does not respect refs passed in
Expand All @@ -85,6 +87,7 @@ function useSetupRefs<T>({
keyToIndexRef,
propsRef,
scrollViewRef,
panRef,
}),
[]
);
Expand Down