Skip to content

Commit 9b4743a

Browse files
committed
Import hooks directly
1 parent 753d43b commit 9b4743a

2 files changed

Lines changed: 21 additions & 30 deletions

File tree

packages/react-native-gesture-handler/src/v3/components/GestureComponents.tsx

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import * as React from 'react';
2-
import {
1+
import React, {
32
PropsWithChildren,
43
ForwardedRef,
54
RefAttributes,
65
ReactElement,
6+
useRef,
7+
useImperativeHandle,
8+
useState,
79
} from 'react';
810
import {
911
ScrollView as RNScrollView,
@@ -23,12 +25,8 @@ import createNativeWrapper, {
2325

2426
import { NativeWrapperProperties } from '../types/NativeWrapperType';
2527
import { NativeWrapperProps } from '../hooks/utils';
26-
import { Gesture } from '../types';
2728
import { DetectorType } from '../detectors';
28-
import {
29-
NativeViewGestureConfig,
30-
NativeViewHandlerData,
31-
} from '../hooks/gestures/native/useNativeGesture';
29+
import { NativeGesture } from '../hooks/gestures/native/useNativeGesture';
3230

3331
export const RefreshControl = createNativeWrapper(
3432
RNRefreshControl,
@@ -57,20 +55,19 @@ export const ScrollView = (
5755
props: RNScrollViewProps &
5856
NativeWrapperProperties & {
5957
ref?: React.RefObject<ImperativeScrollViewRef>;
60-
// This prop existst because using `ref` in `renderScrollComponent` doesn't work.
58+
// This prop existst because using `ref` in `renderScrollComponent` doesn't work (it is overwritten by RN internals).
6159
innerRef?: (node: ImperativeScrollViewRef) => void;
6260
}
6361
) => {
6462
const { refreshControl, innerRef, ref, ...rest } = props;
6563

66-
const [scrollGesture, setScrollGesture] = React.useState<Gesture<
67-
NativeViewHandlerData,
68-
NativeViewGestureConfig
69-
> | null>(null);
64+
const [scrollGesture, setScrollGesture] = useState<NativeGesture | null>(
65+
null
66+
);
7067

71-
const wrapperRef = React.useRef<ComponentWrapperRef<RNScrollViewProps>>(null);
68+
const wrapperRef = useRef<ComponentWrapperRef<RNScrollViewProps>>(null);
7269

73-
React.useImperativeHandle<ImperativeScrollViewRef, ImperativeScrollViewRef>(
70+
useImperativeHandle<ImperativeScrollViewRef, ImperativeScrollViewRef>(
7471
ref,
7572
() => wrapperRef.current
7673
);
@@ -116,20 +113,19 @@ export type TextInput = typeof TextInput & RNTextInput;
116113

117114
type ImperativeFlatListRef =
118115
| (ComponentWrapperRef<RNScrollViewProps> & {
119-
flatListRef: React.RefObject<FlatList<any>>;
116+
flatListRef: React.RefObject<FlatList<unknown>>;
120117
})
121118
| null;
122119

123120
export const FlatList = ((props) => {
124121
const { refreshControl, ref, ...rest } = props;
125122

126-
const [scrollGesture, setScrollGesture] = React.useState<Gesture<
127-
NativeViewHandlerData,
128-
NativeViewGestureConfig
129-
> | null>(null);
123+
const [scrollGesture, setScrollGesture] = useState<NativeGesture | null>(
124+
null
125+
);
130126

131-
const wrapperRef = React.useRef<ImperativeScrollViewRef>(null);
132-
const flatListRef = React.useRef<FlatList<any>>(null);
127+
const wrapperRef = useRef<ImperativeScrollViewRef>(null);
128+
const flatListRef = useRef<FlatList<unknown>>(null);
133129

134130
const flatListProps = {};
135131
const scrollViewProps = {};
@@ -147,7 +143,7 @@ export const FlatList = ((props) => {
147143
}
148144
}
149145

150-
React.useImperativeHandle<ImperativeFlatListRef, ImperativeFlatListRef>(
146+
useImperativeHandle<ImperativeFlatListRef, ImperativeFlatListRef>(
151147
// @ts-ignore We want to override ref
152148
ref,
153149
() => {

packages/react-native-gesture-handler/src/v3/createNativeWrapper.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ import { NativeWrapperProps } from './hooks/utils';
44
import { useNativeGesture } from './hooks/gestures';
55
import { NativeDetector } from './detectors/NativeDetector';
66
import type { NativeWrapperProperties } from './types/NativeWrapperType';
7-
import {
8-
NativeViewGestureConfig,
9-
NativeViewHandlerData,
10-
} from './hooks/gestures/native/useNativeGesture';
11-
import { Gesture } from './types';
7+
import { NativeGesture } from './hooks/gestures/native/useNativeGesture';
128
import { DetectorType, InterceptingGestureDetector } from './detectors';
139
import { VirtualDetector } from './detectors/VirtualDetector/VirtualDetector';
1410

1511
export type ComponentWrapperRef<P> = {
1612
componentRef: React.ComponentType<P>;
17-
gestureRef: Gesture<NativeViewHandlerData, NativeViewGestureConfig>;
13+
gestureRef: NativeGesture;
1814
};
1915

2016
export default function createNativeWrapper<P>(
@@ -51,14 +47,13 @@ export default function createNativeWrapper<P>(
5147
const native = useNativeGesture(gestureHandlerProps);
5248

5349
const componentRef = useRef<React.ComponentType<P>>(null);
54-
const gestureRef = useRef(native);
5550

5651
useImperativeHandle(
5752
props.ref,
5853
() =>
5954
({
6055
componentRef: componentRef.current,
61-
gestureRef: gestureRef.current,
56+
gestureRef: native,
6257
}) as ComponentWrapperRef<P>
6358
);
6459

0 commit comments

Comments
 (0)