Skip to content

Commit 0e23737

Browse files
committed
add ref
1 parent 7edb30e commit 0e23737

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

src/components/DraggableFlatList.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { useCallback, useLayoutEffect, useMemo, useState } from "react";
1+
import React, {
2+
ForwardedRef,
3+
useCallback,
4+
useLayoutEffect,
5+
useMemo,
6+
useState,
7+
} from "react";
28
import {
39
ListRenderItem,
410
FlatListProps,
@@ -57,7 +63,7 @@ function DraggableFlatListInner<T>(props: DraggableFlatListProps<T>) {
5763
const {
5864
cellDataRef,
5965
containerRef,
60-
flatlistRef,
66+
flatListRef,
6167
isTouchActiveRef,
6268
keyToIndexRef,
6369
panGestureHandlerRef,
@@ -395,7 +401,7 @@ function DraggableFlatListInner<T>(props: DraggableFlatListProps<T>) {
395401
<AnimatedFlatList
396402
{...props}
397403
CellRendererComponent={CellRendererComponent}
398-
ref={flatlistRef}
404+
ref={flatListRef}
399405
onContentSizeChange={onListContentSizeChange}
400406
scrollEnabled={!activeKey && scrollEnabled}
401407
renderItem={renderItem}
@@ -422,14 +428,19 @@ function DraggableFlatListInner<T>(props: DraggableFlatListProps<T>) {
422428
);
423429
}
424430

425-
export default function DraggableFlatList<T>(props: DraggableFlatListProps<T>) {
431+
function DraggableFlatList<T>(
432+
props: DraggableFlatListProps<T>,
433+
ref: React.ForwardedRef<FlatList<T>>
434+
) {
426435
return (
427436
<PropsProvider {...props}>
428437
<AnimatedValueProvider>
429-
<RefProvider>
438+
<RefProvider flatListRef={ref}>
430439
<DraggableFlatListInner {...props} />
431440
</RefProvider>
432441
</AnimatedValueProvider>
433442
</PropsProvider>
434443
);
435444
}
445+
446+
export default React.forwardRef(DraggableFlatList);

src/context/refContext.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type RefContextValue<T> = {
1313
cellDataRef: React.MutableRefObject<Map<string, CellData>>;
1414
keyToIndexRef: React.MutableRefObject<Map<string, number>>;
1515
containerRef: React.RefObject<Animated.View>;
16-
flatlistRef: React.RefObject<FlatList<T>>;
16+
flatListRef: React.RefObject<FlatList<T>> | React.ForwardedRef<FlatList<T>>;
1717
panGestureHandlerRef: React.RefObject<PanGestureHandler>;
1818
scrollOffsetRef: React.MutableRefObject<number>;
1919
isTouchActiveRef: React.MutableRefObject<{
@@ -27,10 +27,12 @@ const RefContext = React.createContext<RefContextValue<any> | undefined>(
2727

2828
export default function RefProvider<T>({
2929
children,
30+
flatListRef,
3031
}: {
3132
children: React.ReactNode;
33+
flatListRef: React.ForwardedRef<FlatList<T>>;
3234
}) {
33-
const value = useSetupRefs<T>();
35+
const value = useSetupRefs<T>({ flatListRef });
3436
return <RefContext.Provider value={value}>{children}</RefContext.Provider>;
3537
}
3638

@@ -44,7 +46,11 @@ export function useRefs<T>() {
4446
return value as RefContextValue<T>;
4547
}
4648

47-
function useSetupRefs<T>() {
49+
function useSetupRefs<T>({
50+
flatListRef: flatListRefProp,
51+
}: {
52+
flatListRef: React.ForwardedRef<FlatList<T>>;
53+
}) {
4854
const props = useProps<T>();
4955
const { onRef, animationConfig = DEFAULT_PROPS.animationConfig } = props;
5056

@@ -62,24 +68,21 @@ function useSetupRefs<T>() {
6268
const cellDataRef = useRef(new Map<string, CellData>());
6369
const keyToIndexRef = useRef(new Map<string, number>());
6470
const containerRef = useRef<Animated.View>(null);
65-
const flatlistRef = useRef<FlatList<T>>(null);
71+
const flatListRefInner = useRef<FlatList<T>>(null);
72+
const flatListRef = flatListRefProp || flatListRefInner;
6673
const panGestureHandlerRef = useRef<PanGestureHandler>(null);
6774
const scrollOffsetRef = useRef(0);
6875
const isTouchActiveRef = useRef({
6976
native: isTouchActiveNative,
7077
js: false,
7178
});
7279

73-
useEffect(() => {
74-
if (flatlistRef.current) onRef?.(flatlistRef.current);
75-
}, [onRef]);
76-
7780
const refs = useMemo(
7881
() => ({
7982
animationConfigRef,
8083
cellDataRef,
8184
containerRef,
82-
flatlistRef,
85+
flatListRef,
8386
isTouchActiveRef,
8487
keyToIndexRef,
8588
panGestureHandlerRef,

src/hooks/useAutoScroll.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { useAnimatedValues } from "../context/animatedValueContext";
2626
import { useRefs } from "../context/refContext";
2727

2828
export function useAutoScroll<T>() {
29-
const { flatlistRef } = useRefs();
29+
const { flatListRef } = useRefs<T>();
3030
const {
3131
autoscrollThreshold = DEFAULT_PROPS.autoscrollThreshold,
3232
autoscrollSpeed = DEFAULT_PROPS.autoscrollSpeed,
@@ -111,11 +111,12 @@ export function useAutoScroll<T>() {
111111
isAutoScrollInProgress.current.js = true;
112112

113113
function getFlatListNode(): FlatList<T> | null {
114-
if (!flatlistRef.current) return null;
115-
if ("scrollToOffset" in flatlistRef.current)
116-
return flatlistRef.current as FlatList<T>;
114+
if (!flatListRef || !("current" in flatListRef) || !flatListRef.current)
115+
return null;
116+
if ("scrollToOffset" in flatListRef.current)
117+
return flatListRef.current as FlatList<T>;
117118
//@ts-ignore backwards compat
118-
if ("getNode" in flatlistRef.current) return flatlistRef.getNode();
119+
if ("getNode" in flatListRef.current) return flatListRef.getNode();
119120
return null;
120121
}
121122

0 commit comments

Comments
 (0)