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

Fix useScrollViewOffset taking wrong viewTag #5790

Merged
merged 4 commits into from
Apr 3, 2024
Merged
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
1 change: 1 addition & 0 deletions src/reanimated2/hook/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface AnimatedRef<T extends Component> {
| ShadowNodeWrapper // Fabric
| HTMLElement; // web
current: T | null;
getTag: () => number;
}

// Might make that type generic if it's ever needed.
Expand Down
17 changes: 14 additions & 3 deletions src/reanimated2/hook/useAnimatedRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,20 @@ export function useAnimatedRef<
const getTagValueFunction = isFabric()
? getShadowNodeWrapperFromRef
: findNodeHandle;
tag.value = IS_WEB
? getComponentOrScrollable(component)
: getTagValueFunction(getComponentOrScrollable(component));

const getTagOrShadowNodeWrapper = () => {
return IS_WEB
? getComponentOrScrollable(component)
: getTagValueFunction(getComponentOrScrollable(component));
};

tag.value = getTagOrShadowNodeWrapper();

// On Fabric we have to unwrap the tag from the shadow node wrapper
fun.getTag = isFabric()
? () => findNodeHandle(getComponentOrScrollable(component))
: getTagOrShadowNodeWrapper;

fun.current = component;
// viewName is required only on iOS with Paper
if (Platform.OS === 'ios' && !isFabric()) {
Expand Down
6 changes: 2 additions & 4 deletions src/reanimated2/hook/useScrollViewOffset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
import { useEffect, useRef, useCallback } from 'react';
import type { SharedValue } from '../commonTypes';
import { findNodeHandle } from 'react-native';
import type { EventHandlerInternal } from './useEvent';
import { useEvent } from './useEvent';
import { useSharedValue } from './useSharedValue';
Expand Down Expand Up @@ -103,9 +102,8 @@ function useScrollViewOffsetNative(
}
scrollRef.current = animatedRef.current;

const component = animatedRef.current;
const viewTag = findNodeHandle(component);
eventHandler.workletEventHandler.registerForEvents(viewTag as number);
const viewTag = animatedRef.getTag();
eventHandler.workletEventHandler.registerForEvents(viewTag);
return () => {
eventHandler.workletEventHandler.unregisterFromEvents();
};
Expand Down