Skip to content

Commit 66b83d3

Browse files
committed
refactor: add useCombinedRef to separate the code logically
1 parent 730a941 commit 66b83d3

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/components/KeyboardAwareScrollView/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
useWindowDimensions,
2424
} from "../../hooks";
2525
import { findNodeHandle } from "../../utils/findNodeHandle";
26+
import useCombinedRef from "../hooks/useCombinedRef";
2627

2728
import { useSmoothKeyboardHandler } from "./useSmoothKeyboardHandler";
2829
import { debounce, scrollDistanceWithRespectToSnapPoints } from "./utils";
@@ -127,6 +128,7 @@ const KeyboardAwareScrollView = forwardRef<
127128
) => {
128129
const scrollViewAnimatedRef = useAnimatedRef<Reanimated.ScrollView>();
129130
const scrollViewRef = React.useRef<ScrollView>(null);
131+
const onRef = useCombinedRef(scrollViewAnimatedRef, scrollViewRef);
130132
const scrollViewTarget = useSharedValue<number | null>(null);
131133
const scrollPosition = useSharedValue(0);
132134
const position = useScrollViewOffset(scrollViewAnimatedRef);
@@ -143,11 +145,6 @@ const KeyboardAwareScrollView = forwardRef<
143145

144146
const { height } = useWindowDimensions();
145147

146-
const onRef = useCallback((assignedRef: Reanimated.ScrollView) => {
147-
scrollViewRef.current = assignedRef;
148-
149-
scrollViewAnimatedRef(assignedRef);
150-
}, []);
151148
const onScrollViewLayout = useCallback(
152149
(e: LayoutChangeEvent) => {
153150
scrollViewTarget.value = findNodeHandle(scrollViewAnimatedRef.current);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useCallback } from "react";
2+
3+
import type { Ref, RefCallback } from "react";
4+
5+
const useCombinedRef = <T>(...refs: Ref<T>[]): RefCallback<T> => {
6+
return useCallback((value: T | null) => {
7+
for (const ref of refs) {
8+
if (!ref) {
9+
continue;
10+
}
11+
12+
if (typeof ref === "function") {
13+
ref(value);
14+
} else {
15+
ref.current = value;
16+
}
17+
}
18+
// eslint-disable-next-line react-compiler/react-compiler
19+
}, refs);
20+
};
21+
22+
export default useCombinedRef;

0 commit comments

Comments
 (0)