mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
a6bc77cd94
Summary: When `getItemLayout` is not supplied, VirtualizedList doesn't know the measurements for its child items ahead of time. It caches most recent cell measurements so that an item which is virtualized away is associated with its correct height. VirtualizedList will not consume space for an item it has not yet rendered/knows measurements for. This also means an initially created list will only take up the height of its `initialNumToRender()`. This can cause scroll jumping for the scenario of nested VirtualizedLists of the same orientation, since a virtualized away child list will shrink (compared to the spacer), then re-expand, once returned to. There is logic in the VirtualizedList constructor to load cached frame metrics based on supplied listKey, to enable reloading most recent state. Though locally it seems to be reading the context too early in the lifecycle (compared to other successful checks during componentDidMount, render). It was moved from `componentDidMount` to `constructor` as part of the initial change (https://www.internalfb.com/diff/D6330846 (https://github.com/facebook/react-native/commit/2668dc8e1be7bf93e65ca2e11c87cbeef3310c3e)?dst_version_fbid=131968984136932&transaction_fbid=1578362385612542), and I think has likely been unreliable or dead since. Restoring state was the only usage of a durable key for nested lists beyond their lifetime. So removing the dead code lets us remove the need for a `listKey`, which has been error-prone. This still leaves the problem present since inception, but there may be alternate solutions like scroll anchoring. Changelog: [Internal][Removed] - Remove dead code for restoring state to nested VirtualizedList Reviewed By: ryancat Differential Revision: D39466678 fbshipit-source-id: fc2f39802e5cdf9b920974333be64c74211b99a9
140 lines
3.7 KiB
JavaScript
140 lines
3.7 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow strict-local
|
|
* @format
|
|
*/
|
|
|
|
import * as React from 'react';
|
|
import {useMemo, useContext} from 'react';
|
|
|
|
// Data propagated through nested lists (regardless of orientation) that is
|
|
// useful for producing diagnostics for usage errors involving nesting (e.g
|
|
// missing/duplicate keys).
|
|
export type ListDebugInfo = $ReadOnly<{
|
|
cellKey: string,
|
|
listKey: string,
|
|
parent: ?ListDebugInfo,
|
|
// We include all ancestors regardless of orientation, so this is not always
|
|
// identical to the child's orientation.
|
|
horizontal: boolean,
|
|
}>;
|
|
|
|
type Context = $ReadOnly<{
|
|
cellKey: ?string,
|
|
getScrollMetrics: () => {
|
|
contentLength: number,
|
|
dOffset: number,
|
|
dt: number,
|
|
offset: number,
|
|
timestamp: number,
|
|
velocity: number,
|
|
visibleLength: number,
|
|
zoomScale: number,
|
|
},
|
|
horizontal: ?boolean,
|
|
getOutermostParentListRef: () => React.ElementRef<typeof React.Component>,
|
|
registerAsNestedChild: ({
|
|
cellKey: string,
|
|
key: string,
|
|
ref: React.ElementRef<typeof React.Component>,
|
|
parentDebugInfo: ListDebugInfo,
|
|
}) => void,
|
|
unregisterAsNestedChild: ({
|
|
key: string,
|
|
}) => void,
|
|
debugInfo: ListDebugInfo,
|
|
}>;
|
|
|
|
export const VirtualizedListContext: React.Context<?Context> =
|
|
React.createContext(null);
|
|
if (__DEV__) {
|
|
VirtualizedListContext.displayName = 'VirtualizedListContext';
|
|
}
|
|
|
|
/**
|
|
* Resets the context. Intended for use by portal-like components (e.g. Modal).
|
|
*/
|
|
export function VirtualizedListContextResetter({
|
|
children,
|
|
}: {
|
|
children: React.Node,
|
|
}): React.Node {
|
|
return (
|
|
<VirtualizedListContext.Provider value={null}>
|
|
{children}
|
|
</VirtualizedListContext.Provider>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Sets the context with memoization. Intended to be used by `VirtualizedList`.
|
|
*/
|
|
export function VirtualizedListContextProvider({
|
|
children,
|
|
value,
|
|
}: {
|
|
children: React.Node,
|
|
value: Context,
|
|
}): React.Node {
|
|
// Avoid setting a newly created context object if the values are identical.
|
|
const context = useMemo(
|
|
() => ({
|
|
cellKey: null,
|
|
getScrollMetrics: value.getScrollMetrics,
|
|
horizontal: value.horizontal,
|
|
getOutermostParentListRef: value.getOutermostParentListRef,
|
|
registerAsNestedChild: value.registerAsNestedChild,
|
|
unregisterAsNestedChild: value.unregisterAsNestedChild,
|
|
debugInfo: {
|
|
cellKey: value.debugInfo.cellKey,
|
|
horizontal: value.debugInfo.horizontal,
|
|
listKey: value.debugInfo.listKey,
|
|
parent: value.debugInfo.parent,
|
|
},
|
|
}),
|
|
[
|
|
value.getScrollMetrics,
|
|
value.horizontal,
|
|
value.getOutermostParentListRef,
|
|
value.registerAsNestedChild,
|
|
value.unregisterAsNestedChild,
|
|
value.debugInfo.cellKey,
|
|
value.debugInfo.horizontal,
|
|
value.debugInfo.listKey,
|
|
value.debugInfo.parent,
|
|
],
|
|
);
|
|
return (
|
|
<VirtualizedListContext.Provider value={context}>
|
|
{children}
|
|
</VirtualizedListContext.Provider>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Sets the `cellKey`. Intended to be used by `VirtualizedList` for each cell.
|
|
*/
|
|
export function VirtualizedListCellContextProvider({
|
|
cellKey,
|
|
children,
|
|
}: {
|
|
cellKey: string,
|
|
children: React.Node,
|
|
}): React.Node {
|
|
// Avoid setting a newly created context object if the values are identical.
|
|
const currContext = useContext(VirtualizedListContext);
|
|
const context = useMemo(
|
|
() => (currContext == null ? null : {...currContext, cellKey}),
|
|
[currContext, cellKey],
|
|
);
|
|
return (
|
|
<VirtualizedListContext.Provider value={context}>
|
|
{children}
|
|
</VirtualizedListContext.Provider>
|
|
);
|
|
}
|