Skip to content

Commit

Permalink
Fix type definition of useMergeRefs
Browse files Browse the repository at this point in the history
Summary:
The type definition of `useMergeRefs` is incorrect, which forces all callsites to use `$FlowFixMe`. This fixes the definition and removes all the `$FlowFixMe`s caused by it.

Changelog: [internal]

Differential Revision: D51660716
  • Loading branch information
rubennorte authored and facebook-github-bot committed Nov 29, 2023
1 parent a04bc20 commit 27bf325
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export default function createAnimatedComponent<TProps: {...}, TInstance>(
// $FlowFixMe[incompatible-call]
props,
);
// $FlowFixMe[incompatible-call]
const ref = useMergeRefs<TInstance | null>(callbackRef, forwardedRef);

// Some components require explicit passthrough values for animation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const ScrollViewStickyHeaderWithForwardedRef: React.AbstractComponent<
}, []);
const ref: (React.ElementRef<typeof Animated.View> | null) => void =
// $FlowFixMe[incompatible-type] - Ref is mutated by `callbackRef`.
// $FlowFixMe[incompatible-call]
useMergeRefs<Instance | null>(callbackRef, forwardedRef);

const offset = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ const SwitchWithForwardedRef: React.AbstractComponent<
typeof SwitchNativeComponent | typeof AndroidSwitchNativeComponent,
> | null>(null);

// $FlowFixMe[incompatible-call]
const ref = useMergeRefs(nativeSwitchRef, forwardedRef);

const [native, setNative] = React.useState({value: (null: ?boolean)});
Expand Down
2 changes: 0 additions & 2 deletions packages/react-native/Libraries/Image/ImageInjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ export function useWrapRefWithImageAttachedCallbacks(

// `useMergeRefs` returns a stable ref if its arguments don't change.
return useMergeRefs<React.ElementRef<AbstractImageAndroid> | null>(
// $FlowFixMe[incompatible-call]
forwardedRef,
// $FlowFixMe[incompatible-call]
imageAttachedCallbacksRef.current,
);
}
14 changes: 5 additions & 9 deletions packages/react-native/Libraries/Utilities/useMergeRefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
* @format
*/

import * as React from 'react';
import {useCallback} from 'react';

type CallbackRef<T> = T => mixed;
type ObjectRef<T> = {current: T, ...};

type Ref<T> = CallbackRef<T> | ObjectRef<T>;

/**
* Constructs a new ref that forwards new values to each of the given refs. The
* given refs will always be invoked in the order that they are supplied.
Expand All @@ -24,11 +20,11 @@ type Ref<T> = CallbackRef<T> | ObjectRef<T>;
* the returned callback ref is supplied as a `ref` to a React element, this may
* lead to problems with the given refs being invoked more times than desired.
*/
export default function useMergeRefs<T>(
...refs: $ReadOnlyArray<?Ref<T>>
): CallbackRef<T> {
export default function useMergeRefs<Instance>(
...refs: $ReadOnlyArray<?React.RefSetter<Instance>>
): (Instance | null) => void {
return useCallback(
(current: T) => {
(current: Instance | null) => {
for (const ref of refs) {
if (ref != null) {
if (typeof ref === 'function') {
Expand Down

0 comments on commit 27bf325

Please sign in to comment.