VirtualizedList up-to-date state: Thread props to _updateViewableItems()

Summary:
This diff is part of an overall stack, meant to fix incorrect usage of `setState()` in `VirtualizedList`, which triggers new invariant checks added in `VirtualizedList_EXPERIMENTAL`. See the stack summary below for more information on the broader change.

## Diff Summary

Change `_updateViewableItems()` to accept an explicit set of props, and a CellRenderMask, instead of using `this.props` and `this.state`. The eventual sink are the `_getFrameMetric*` functions, so a minimal projection of props is added that we can pass through to it.

## Stack Summary
`VirtualizedList`'s component state is a set of cells to render. This state is set via the `setState()` class component API. The main "tick" function `VirtualizedList._updateCellsToRender()` calculates this new state using a combination of the current component state, and instance-local state like maps, measurement caches, etc.

From: https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous
 ---
> React may batch multiple setState() calls into a single update for performance. Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state. For example, this code may fail to update the counter:

```
// Wrong
this.setState({
  counter: this.state.counter + this.props.increment,
});
```
> To fix it, use a second form of setState() that accepts a function rather than an object. That function will receive the previous state as the first argument, and the props at the time the update is applied as the second argument:
```
// Correct
this.setState((state, props) => ({
  counter: state.counter + props.increment
}));
```
 ---
`_updateCellsToRender()` transitively calls many functions which will read directly from `this.props` or `this.state` instead of the value passed by the state updater. This intermittently fires invariant violations, when there is a mismatch.

This diff migrates all usages of `props` and `state` during state update to the values provied in `setState()`. To prevent future mismatch, and to provide better clarity on when it is safe to use `this.props`, `this.state`, I overrode `setState` to fire an invariant violation if it is accessed when it is unsafe to:

{F756963772}

Changelog:
[Internal][Changed] - Move Props to VirtualizedListProps

Reviewed By: genkikondo

Differential Revision: D38293587

fbshipit-source-id: 164fd4af7c370d905af53b0e9aafb3592d8659cc
This commit is contained in:
Nick Gerleman 2022-08-02 01:30:21 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 0538f45e45
Коммит dd9e896645
2 изменённых файлов: 24 добавлений и 9 удалений

Просмотреть файл

@ -271,3 +271,14 @@ export type Props = {|
...RequiredProps,
...OptionalProps,
|};
/**
* Subset of properties needed to calculate frame metrics
*/
export type FrameMetricProps = {
data: RequiredProps['data'],
getItemCount: RequiredProps['getItemCount'],
getItem: RequiredProps['getItem'],
getItemLayout?: OptionalProps['getItemLayout'],
...
};

Просмотреть файл

@ -18,6 +18,7 @@ import type {
import type {ViewToken} from './ViewabilityHelper';
import type {
FrameMetricProps,
Item,
Props,
RenderItemProps,
@ -318,7 +319,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
this._viewabilityTuples.forEach(t => {
t.viewabilityHelper.recordInteraction();
});
this._updateViewableItems(this.props.data);
this._updateViewableItems(this.props, this.state.cellsAroundViewport);
}
flashScrollIndicators() {
@ -595,7 +596,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
const onEndReachedThreshold = onEndReachedThresholdOrDefault(
props.onEndReachedThreshold,
);
this._updateViewableItems(data);
this._updateViewableItems(props, cellsAroundViewport);
const {contentLength, offset, visibleLength} = this._scrollMetrics;
const distanceFromEnd = contentLength - visibleLength - offset;
@ -713,7 +714,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
},
});
}
this._updateViewableItems(null);
this._updateViewableItems(null, this.state.cellsAroundViewport);
this._updateCellsToRenderBatcher.dispose({abort: true});
this._viewabilityTuples.forEach(tuple => {
tuple.viewabilityHelper.dispose();
@ -1265,7 +1266,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
this._triggerRemeasureForChildListsInCell(cellKey);
this._computeBlankness();
this._updateViewableItems(this.props.data);
this._updateViewableItems(this.props, this.state.cellsAroundViewport);
};
_onCellFocusCapture(cellKey: string) {
@ -1613,7 +1614,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
visibleLength,
zoomScale,
};
this._updateViewableItems(this.props.data);
this._updateViewableItems(this.props, this.state.cellsAroundViewport);
if (!this.props) {
return;
}
@ -1847,18 +1848,21 @@ class VirtualizedList extends React.PureComponent<Props, State> {
return [{first, last}];
};
_updateViewableItems(data: any) {
const {getItemCount} = this.props;
_updateViewableItems(
props: ?FrameMetricProps,
cellsAroundViewport: {first: number, last: number},
) {
const itemCount = props ? props.getItemCount(props.data) : 0;
this._viewabilityTuples.forEach(tuple => {
tuple.viewabilityHelper.onUpdate(
getItemCount(data),
itemCount,
this._scrollMetrics.offset,
this._scrollMetrics.visibleLength,
this._getFrameMetrics,
this._createViewToken,
tuple.onViewableItemsChanged,
this.state.cellsAroundViewport,
cellsAroundViewport,
);
});
}