Skip to content

Fix ref management for Reanimated 4 #3511

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion src/handlers/gestures/GestureDetector/Wrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
onGestureHandlerEvent?: unknown;
// Implicit `children` prop has been removed in @types/react^18.0.0
children?: React.ReactNode;
reanimatedContext?: { current: number };
}> {
render() {
try {
Expand All @@ -17,7 +18,10 @@
const child: any = React.Children.only(this.props.children);
return React.cloneElement(
child,
{ collapsable: false },
{

Check failure on line 21 in src/handlers/gestures/GestureDetector/Wrap.tsx

View workflow job for this annotation

GitHub Actions / check

Delete `·`
collapsable: false,
reanimatedContext: this.props.reanimatedContext,
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
child.props.children
);
Expand Down
16 changes: 13 additions & 3 deletions src/handlers/gestures/GestureDetector/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable react/no-unused-prop-types */
import React, {
Ref,
useContext,
useEffect,
useLayoutEffect,
Expand Down Expand Up @@ -114,7 +115,10 @@

const webEventHandlersRef = useWebEventHandlers();
// Store state in ref to prevent unnecessary renders
const state = useRef<GestureDetectorState>({
const state = useRef<
GestureDetectorState

Check failure on line 119 in src/handlers/gestures/GestureDetector/index.tsx

View workflow job for this annotation

GitHub Actions / check

Insert `&·{`
& { viewRef: { props: { reanimatedContext?: { current: number } } } | null }

Check failure on line 120 in src/handlers/gestures/GestureDetector/index.tsx

View workflow job for this annotation

GitHub Actions / check

Replace `&·{·viewRef:·{·props:·{·reanimatedContext?:·{·current:·number·}·}·}·|·null` with `··viewRef:·{·props:·{·reanimatedContext?:·{·current:·number·}·}·}·|·null;⏎···`
>({
firstRender: true,
viewRef: null,
previousViewTag: -1,
Expand Down Expand Up @@ -150,7 +154,13 @@
useAnimatedGesture(preparedGesture, needsToRebuildReanimatedEvent);

useLayoutEffect(() => {
const viewTag = findNodeHandle(state.viewRef) as number;
let viewTag = -1;
const reanimatedContext = state.viewRef?.props.reanimatedContext;
if (reanimatedContext && reanimatedContext.current > 0) {
viewTag = reanimatedContext.current;
} else {
viewTag = findNodeHandle(state.viewRef) as number;
}
preparedGesture.isMounted = true;

attachHandlers({
Expand Down Expand Up @@ -186,6 +196,6 @@
</AnimatedWrap>
);
} else {
return <Wrap ref={refHandler}>{props.children}</Wrap>;
return <Wrap ref={refHandler as Ref<Wrap>}>{props.children}</Wrap>;
}
};
13 changes: 11 additions & 2 deletions src/handlers/gestures/GestureDetector/useViewRefHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
isViewFlatteningDisabled: (node: unknown) => boolean | null; // JSI function
};

type Props = {
reanimatedContext?: { current: number },

Check failure on line 13 in src/handlers/gestures/GestureDetector/useViewRefHandler.ts

View workflow job for this annotation

GitHub Actions / check

Expected a semicolon

Check failure on line 13 in src/handlers/gestures/GestureDetector/useViewRefHandler.ts

View workflow job for this annotation

GitHub Actions / check

Replace `,` with `;`
}

Check failure on line 14 in src/handlers/gestures/GestureDetector/useViewRefHandler.ts

View workflow job for this annotation

GitHub Actions / check

Insert `;`

// Ref handler for the Wrap component attached under the GestureDetector.
// It's responsible for setting the viewRef on the state and triggering the reattaching of handlers
// if the view has changed.
Expand All @@ -17,7 +21,7 @@
updateAttachedGestures: (skipConfigUpdate?: boolean) => void
) {
const refHandler = useCallback(
(ref: React.Component | null) => {
(ref: (React.Component<Props>)) => {

Check failure on line 24 in src/handlers/gestures/GestureDetector/useViewRefHandler.ts

View workflow job for this annotation

GitHub Actions / check

Replace `(React.Component<Props>)` with `React.Component<Props>`
if (ref === null) {
return;
}
Expand All @@ -26,7 +30,12 @@

// if it's the first render, also set the previousViewTag to prevent reattaching gestures when not needed
if (state.previousViewTag === -1) {
state.previousViewTag = findNodeHandle(state.viewRef) as number;
const reanimatedContext = ref.props.reanimatedContext;
if (reanimatedContext && reanimatedContext.current > 0) {
state.previousViewTag = reanimatedContext.current;
} else {
state.previousViewTag = findNodeHandle(state.viewRef) as number;
}
}

// Pass true as `skipConfigUpdate`. Here we only want to trigger the eventual reattaching of handlers
Expand Down
Loading