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

Refactor findNodeHandler #6736

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

piaskowyk
Copy link
Member

@piaskowyk piaskowyk commented Nov 20, 2024

Summary

This PR addresses the issue reported at #6719 and aims to:

  • Unify the method used to obtain the view tag, as there are currently several approaches.
  • Avoid passing a class component to the findNodeHandler. Instead, we'll pass a ref to the component, similar to what Expo implemented here: [core] Use findNodeHandle with a native ref instead of class component instance expo/expo#33016.
  • Limit unnecessary invocations of findNodeHandler to no more than one call per render.
  • Remove the invocation of findHostInstance from Paper renderer on the New Architecture.

Additional Remarks:

  • When a class component is passed to createAnimatedComponent, it will still fall back to the slow path where we can get a native ref.
  • In NativeEventManager, we need to call findNodeHandler again after every render to ensure that the children of <GestureDetector /> haven't changed their native tags.
  • LayoutAnimationConfig still uses findNodeHandler. It requires a complete refactor of their functionality to eliminate its use, and I plan to handle this in another PR.
  • findHostInstance_DEPRECATED always follows the slow path even for native refs, which is why I've implemented our own version of findHostInstance to optimize the happy path.

Fixes #6719

Related PRs:

Test plan

  • check Paper
  • check Fabric
  • check Web

Copy link
Collaborator

@tjzel tjzel left a comment

Choose a reason for hiding this comment

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

Looks good to me overall. Make sure to test it with the newest Gesture Handler and SVG on their more complex examples, since they used to have cases where these refs mechanisms broke.

@piaskowyk piaskowyk marked this pull request as ready for review November 22, 2024 09:07
? this.#managedComponent
: componentAnimatedRef
) ?? -1;
if (typeof scrollableNode === 'number') {
Copy link
Member

Choose a reason for hiding this comment

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

Does this also work as intended on Fabric where there are shadow node wrappers instead of view tags (numbers)?

Copy link
Member Author

Choose a reason for hiding this comment

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

On both platforms, getScrollableNode returns a number - I've checked it for both ScrollView and FlatList.

packages/react-native-reanimated/src/fabricUtils.ts Outdated Show resolved Hide resolved
// This is a native ref to a Paper component
return ref;
}
return undefined;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we throw an error in such case for easier debugging?

Copy link
Collaborator

Choose a reason for hiding this comment

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

When findHostInstanceFastPath returns undefined we call findHostInstance_DEPRECATED()

Copy link
Member Author

Choose a reason for hiding this comment

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

This is not an error. That just means it’s a ref to a non-native component, and it’s necessary to call findHostInstance_DEPRECATED on them - I'll add a proper comment to code to explain it.

Comment on lines 28 to 30
} catch (e) {
findHostInstance_DEPRECATED = (_ref: unknown) => null;
}
Copy link
Member

Choose a reason for hiding this comment

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

When will that happen? Shouldn't we throw an error indeed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the previous behavior. I wonder if we should change it 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I've just followed the previous implementation, but it might be more reasonable to throw an error instead


resolveFindHostInstance_DEPRECATED();
return findHostInstance_DEPRECATED(
isFabric() ? component : component._componentRef
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we can skip this isFabric() call here and just call a function that uses findHostInstance_DEPRECATED under the hood.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you mean?

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we can call isFabric() only once to determine which implementation should be called but now I think there's a risk that isFabric() will be called too early.

// This is a native ref to a Paper component
return ref;
}
return undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

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

When findHostInstanceFastPath returns undefined we call findHostInstance_DEPRECATED()

Comment on lines 28 to 30
} catch (e) {
findHostInstance_DEPRECATED = (_ref: unknown) => null;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the previous behavior. I wonder if we should change it 🤔


resolveFindHostInstance_DEPRECATED();
return findHostInstance_DEPRECATED(
isFabric() ? component : component._componentRef
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Renimated calls findNodeHandle with class instances, which is a slow path in React
3 participants