From f657d2906d456e88c997564378915beb67ef1eeb Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Sat, 12 Nov 2022 09:21:43 -0800 Subject: [PATCH] RN: Inline `setAndForwardRef` into `createAnimatedComponent.js` Summary: Inlines `setAndForwardRef` into `createAnimatedComponent.js`. I am planning to delete `setAndForwardRef` because it encourages a subtle bad practice with management of the referential equality of `ref` entities. I am doing this instead of refactoring `createAnimatedComponent` because this legacy implementation is planned to be replaced very soon. Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D41205066 fbshipit-source-id: dc481e73a6c4d6acbae530d4da48b3a032575179 --- Libraries/Animated/createAnimatedComponent.js | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Libraries/Animated/createAnimatedComponent.js b/Libraries/Animated/createAnimatedComponent.js index 15072c0c7cb..0690aeec241 100644 --- a/Libraries/Animated/createAnimatedComponent.js +++ b/Libraries/Animated/createAnimatedComponent.js @@ -11,7 +11,6 @@ 'use strict'; import View from '../Components/View/View'; -import setAndForwardRef from '../Utilities/setAndForwardRef'; import {AnimatedEvent} from './AnimatedEvent'; import * as createAnimatedComponentInjection from './createAnimatedComponentInjection'; import NativeAnimatedHelper from './NativeAnimatedHelper'; @@ -275,4 +274,27 @@ function createAnimatedComponent( }); } +function setAndForwardRef({ + getForwardedRef, + setLocalRef, +}: $ReadOnly<{| + getForwardedRef: () => ?React.Ref, + setLocalRef: (ref: React.ElementRef) => mixed, +|}>): (ref: React.ElementRef) => void { + return function forwardRef(ref: React.ElementRef) { + const forwardedRef = getForwardedRef(); + + setLocalRef(ref); + + // Forward to user ref prop (if one has been specified) + if (typeof forwardedRef === 'function') { + // Handle function-based refs. String-based refs are handled as functions. + forwardedRef(ref); + } else if (typeof forwardedRef === 'object' && forwardedRef != null) { + // Handle createRef-based refs + forwardedRef.current = ref; + } + }; +} + export default (createAnimatedComponent: typeof createAnimatedComponent);