Skip to content

Commit

Permalink
Allow VirtualizedListContext to support different component type
Browse files Browse the repository at this point in the history
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 facebook@b2f871a). 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
  • Loading branch information
NickGerleman authored and roryabraham committed Aug 17, 2022
1 parent 5986fe3 commit 4d9664e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 6 additions & 4 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,11 @@ class VirtualizedList extends React.PureComponent<Props, State> {
_registerAsNestedChild = (childList: {
cellKey: string,
key: string,
ref: VirtualizedList,
ref: React.ElementRef<typeof React.Component>,
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();
Expand All @@ -651,19 +652,20 @@ class VirtualizedList extends React.PureComponent<Props, State> {
'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();
}
};

Expand Down
5 changes: 2 additions & 3 deletions Libraries/Lists/VirtualizedListContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* @format
*/

import type VirtualizedList from './VirtualizedList.js';
import * as React from 'react';
import {useMemo, useContext} from 'react';

Expand Down Expand Up @@ -50,12 +49,12 @@ type Context = $ReadOnly<{
zoomScale: number,
},
horizontal: ?boolean,
getOutermostParentListRef: () => VirtualizedList,
getOutermostParentListRef: () => React.ElementRef<typeof React.Component>,
getNestedChildState: string => ?ChildListState,
registerAsNestedChild: ({
cellKey: string,
key: string,
ref: VirtualizedList,
ref: React.ElementRef<typeof React.Component>,
parentDebugInfo: ListDebugInfo,
}) => ?ChildListState,
unregisterAsNestedChild: ({
Expand Down

0 comments on commit 4d9664e

Please sign in to comment.