From 4d9664ee69c8f65597b4c2a4612bb5cf091e624f Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 26 Jul 2022 20:06:58 -0700 Subject: [PATCH] Allow VirtualizedListContext to support different component type Summary: This change is in preparation of adding a separate `VirtualizedList_EXPERIMENTAL` component. Both the original, and experimental lists use `VirtualizedListContext`, which itself references back to the VirtualizedList class type. VirtualizedList private methods are currently included in the type system, and are called in other VirtualizedList code (see https://github.com/facebook/react-native/commit/b2f871a6fa9c92dd0712055384b9eca6d828e37d). This prevents Flow from seeing the two classes are compatible if "private" methods change. My first attempt was to parameterize the context, to allow both `VirtualizedList`, and `VirtualizedList_EXPERIMENTAL` to use the same code without sacrificing type safety or adding further duplication. This added more complexity than it is worth, so I am instead loosening the type on VirtualizedListContext to pass around a more generic handle. Changelog: [Internal][Changed] - Allow VirtualizedListContext to support different component type Reviewed By: javache Differential Revision: D38017086 fbshipit-source-id: 91e8f6ab2591d3ae9b7f9263711b4a39b78f68e0 --- Libraries/Lists/VirtualizedList.js | 10 ++++++---- Libraries/Lists/VirtualizedListContext.js | 5 ++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index 69e63090ea32d5..ebd06871906c49 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -634,10 +634,11 @@ class VirtualizedList extends React.PureComponent { _registerAsNestedChild = (childList: { cellKey: string, key: string, - ref: VirtualizedList, + ref: React.ElementRef, parentDebugInfo: ListDebugInfo, ... }): ?ChildListState => { + const specificRef = ((childList.ref: any): VirtualizedList); // Register the mapping between this child key and the cellKey for its cell const childListsInCell = this._cellKeysToChildListKeys.get(childList.cellKey) || new Set(); @@ -651,19 +652,20 @@ class VirtualizedList extends React.PureComponent { 'list. You must pass a unique listKey prop to each sibling list.\n\n' + describeNestedLists({ ...childList, + ref: specificRef, // We're called from the child's componentDidMount, so it's safe to // read the child's props here (albeit weird). - horizontal: !!childList.ref.props.horizontal, + horizontal: !!specificRef.props.horizontal, }), ); } this._nestedChildLists.set(childList.key, { - ref: childList.ref, + ref: specificRef, state: null, }); if (this._hasInteracted) { - childList.ref.recordInteraction(); + specificRef.recordInteraction(); } }; diff --git a/Libraries/Lists/VirtualizedListContext.js b/Libraries/Lists/VirtualizedListContext.js index 308f6a20d19a54..a17c4b19160a1b 100644 --- a/Libraries/Lists/VirtualizedListContext.js +++ b/Libraries/Lists/VirtualizedListContext.js @@ -8,7 +8,6 @@ * @format */ -import type VirtualizedList from './VirtualizedList.js'; import * as React from 'react'; import {useMemo, useContext} from 'react'; @@ -50,12 +49,12 @@ type Context = $ReadOnly<{ zoomScale: number, }, horizontal: ?boolean, - getOutermostParentListRef: () => VirtualizedList, + getOutermostParentListRef: () => React.ElementRef, getNestedChildState: string => ?ChildListState, registerAsNestedChild: ({ cellKey: string, key: string, - ref: VirtualizedList, + ref: React.ElementRef, parentDebugInfo: ListDebugInfo, }) => ?ChildListState, unregisterAsNestedChild: ({