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
29 changes: 23 additions & 6 deletions src/reanimated2/hook/useAnimatedRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
} from '../shareables';

interface ComponentRef extends Component {
displayName?: string;
name?: string;
mstach60161 marked this conversation as resolved.
Show resolved Hide resolved
getNativeScrollRef?: () => ComponentRef;
getScrollableNode?: () => ComponentRef;
}

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

function getScrollableRef(component: ComponentRef): ComponentRef {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getScrollableRef -> getComponentRef

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getComponentRef is too generic I think

if (global._IS_FABRIC) {
if (component.getNativeScrollRef) {
return component.getNativeScrollRef();
} else {
console.warn(
`[Reanimated] ${
component.displayName || component.name || 'Component'
} has no implemented 'getNativeScrollRef' method. Please report this issue to the library maintainers.`
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to remove this config because we can pass here reference to any object.

} else {
if (component.getScrollableNode) {
return component.getScrollableNode();
}
}
return component;
}

const getTagValueFunction = global._IS_FABRIC
? getShareableShadowNodeFromComponent
: getTag;
Expand All @@ -31,11 +53,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(getScrollableRef(component));
mstach60161 marked this conversation as resolved.
Show resolved Hide resolved
fun.current = component;
}
return tag.value;
Expand All @@ -50,7 +68,6 @@ export function useAnimatedRef<T extends ComponentRef>(): RefObjectFunction<T> {
},
});
registerShareableMapping(fun, remoteRef);

ref.current = fun;
}

Expand Down