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 animatedRef on Fabric #4445

Merged
merged 9 commits into from
May 22, 2023
17 changes: 11 additions & 6 deletions src/reanimated2/hook/useAnimatedRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../shareables';

interface ComponentRef extends Component {
getNativeScrollRef?: () => ComponentRef;
getScrollableNode?: () => ComponentRef;
}

Expand All @@ -19,6 +20,15 @@ function getShareableShadowNodeFromComponent(
return getShadowNodeWrapperFromHostInstance(component);
}

function getComponentOrScrollableRef(component: ComponentRef): ComponentRef {
if (global._IS_FABRIC && component.getNativeScrollRef) {
return component.getNativeScrollRef();
} else if (!global._IS_FABRIC && component.getScrollableNode) {
return component.getScrollableNode();
}
return component;
}

const getTagValueFunction = global._IS_FABRIC
? getShareableShadowNodeFromComponent
: getTag;
Expand All @@ -31,11 +41,7 @@ export function useAnimatedRef<T extends ComponentRef>(): RefObjectFunction<T> {
const fun: RefObjectFunction<T> = <RefObjectFunction<T>>((component) => {
// enters when ref is set by attaching to a component
if (component) {
tag.value = getTagValueFunction(
component.getScrollableNode
? component.getScrollableNode()
: component
);
tag.value = getTagValueFunction(getComponentOrScrollableRef(component));
fun.current = component;
}
return tag.value;
Expand All @@ -50,7 +56,6 @@ export function useAnimatedRef<T extends ComponentRef>(): RefObjectFunction<T> {
},
});
registerShareableMapping(fun, remoteRef);

ref.current = fun;
}

Expand Down