Skip to content

Commit

Permalink
RN: Inline setAndForwardRef into createAnimatedComponent.js
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yungsters authored and facebook-github-bot committed Nov 12, 2022
1 parent 666f56b commit f657d29
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Libraries/Animated/createAnimatedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);

0 comments on commit f657d29

Please sign in to comment.