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
This commit is contained in:
Tim Yung
2022-11-12 09:21:43 -08:00
committed by Facebook GitHub Bot
parent 666f56bff3
commit f657d2906d
+23 -1
View File
@@ -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<Props: {+[string]: mixed, ...}, Instance>(
});
}
function setAndForwardRef({
getForwardedRef,
setLocalRef,
}: $ReadOnly<{|
getForwardedRef: () => ?React.Ref<any>,
setLocalRef: (ref: React.ElementRef<any>) => mixed,
|}>): (ref: React.ElementRef<any>) => void {
return function forwardRef(ref: React.ElementRef<any>) {
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);