From 4fe1844debd01ee08f64fe2702cc259ade90f8dc Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 22 Nov 2024 12:54:31 +0000 Subject: [PATCH] Replace findNodeHandle with reading _componentViewTag (#6720) ## Summary Partially addresses https://github.com/software-mansion/react-native-reanimated/issues/6719. This is better because it doesn't enter the traversal codepath inside React. The `findNodeHandle` calculation inside the `Animated.View` itself is not so bad because it's short circuited inside for native refs (at least with `View`). Whereas passing a class instance like here always triggers the slower path in React. Regardless, it's already computed so why compute again? ## Test plan I've added console logs in our app to verify whether `animatedComponent._componentViewTag === findNodeHandle(animatedComponent)` and it always turned out true in the cases I hit. Not sure if there any cases where those could be different. Co-authored-by: Krzysztof Piaskowy --- .../src/createAnimatedComponent/JSPropsUpdater.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/react-native-reanimated/src/createAnimatedComponent/JSPropsUpdater.ts b/packages/react-native-reanimated/src/createAnimatedComponent/JSPropsUpdater.ts index 59bc5a75a87a..f0bd87da0363 100644 --- a/packages/react-native-reanimated/src/createAnimatedComponent/JSPropsUpdater.ts +++ b/packages/react-native-reanimated/src/createAnimatedComponent/JSPropsUpdater.ts @@ -1,6 +1,5 @@ 'use strict'; import { NativeEventEmitter, Platform } from 'react-native'; -import { findNodeHandle } from '../platformFunctions/findNodeHandle'; import type { NativeModule } from 'react-native'; import { shouldBeUseWeb } from '../PlatformChecker'; import type { StyleProps } from '../commonTypes'; @@ -39,7 +38,7 @@ class JSPropsUpdaterPaper implements IJSPropsUpdater { > & IAnimatedComponentInternal ) { - const viewTag = findNodeHandle(animatedComponent); + const viewTag = animatedComponent._componentViewTag; JSPropsUpdaterPaper._tagToComponentMapping.set(viewTag, animatedComponent); if (JSPropsUpdaterPaper._tagToComponentMapping.size === 1) { const listener = (data: ListenerData) => { @@ -61,7 +60,7 @@ class JSPropsUpdaterPaper implements IJSPropsUpdater { > & IAnimatedComponentInternal ) { - const viewTag = findNodeHandle(animatedComponent); + const viewTag = animatedComponent._componentViewTag; JSPropsUpdaterPaper._tagToComponentMapping.delete(viewTag); if (JSPropsUpdaterPaper._tagToComponentMapping.size === 0) { this._reanimatedEventEmitter.removeAllListeners( @@ -101,7 +100,7 @@ class JSPropsUpdaterFabric implements IJSPropsUpdater { if (!JSPropsUpdaterFabric.isInitialized) { return; } - const viewTag = findNodeHandle(animatedComponent); + const viewTag = animatedComponent._componentViewTag; JSPropsUpdaterFabric._tagToComponentMapping.set(viewTag, animatedComponent); } @@ -114,7 +113,7 @@ class JSPropsUpdaterFabric implements IJSPropsUpdater { if (!JSPropsUpdaterFabric.isInitialized) { return; } - const viewTag = findNodeHandle(animatedComponent); + const viewTag = animatedComponent._componentViewTag; JSPropsUpdaterFabric._tagToComponentMapping.delete(viewTag); } }