Skip to content

Commit

Permalink
VirtualizedList up-to-date state: Thread props to _createViewToken()
Browse files Browse the repository at this point in the history
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

`_createViewToken()` is called during state changes. Use explicit props passed in.

## 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][Fixed] - Thread props to _createViewToken()

Reviewed By: genkikondo

Differential Revision: D38294339

fbshipit-source-id: dc215e267f126a3789742c14c35479f509822710
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Aug 2, 2022
1 parent e3aad65 commit a53512f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
17 changes: 14 additions & 3 deletions Libraries/Lists/ViewabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ class ViewabilityHelper {
offset: number,
...
},
createViewToken: (index: number, isViewable: boolean) => ViewToken,
createViewToken: (
index: number,
isViewable: boolean,
props: FrameMetricProps,
) => ViewToken,
onViewableItemsChanged: ({
viewableItems: Array<ViewToken>,
changed: Array<ViewToken>,
Expand Down Expand Up @@ -236,6 +240,7 @@ class ViewabilityHelper {
* see the error delete this comment and run Flow. */
this._timers.delete(handle);
this._onUpdateSync(
props,
viewableIndices,
onViewableItemsChanged,
createViewToken,
Expand All @@ -247,6 +252,7 @@ class ViewabilityHelper {
this._timers.add(handle);
} else {
this._onUpdateSync(
props,
viewableIndices,
onViewableItemsChanged,
createViewToken,
Expand All @@ -269,13 +275,18 @@ class ViewabilityHelper {
}

_onUpdateSync(
props: FrameMetricProps,
viewableIndicesToCheck: Array<number>,
onViewableItemsChanged: ({
changed: Array<ViewToken>,
viewableItems: Array<ViewToken>,
...
}) => void,
createViewToken: (index: number, isViewable: boolean) => ViewToken,
createViewToken: (
index: number,
isViewable: boolean,
props: FrameMetricProps,
) => ViewToken,
) {
// Filter out indices that have gone out of view since this call was scheduled.
viewableIndicesToCheck = viewableIndicesToCheck.filter(ii =>
Expand All @@ -284,7 +295,7 @@ class ViewabilityHelper {
const prevItems = this._viewableItems;
const nextItems = new Map(
viewableIndicesToCheck.map(ii => {
const viewable = createViewToken(ii, true);
const viewable = createViewToken(ii, true, props);
return [viewable.key, viewable];
}),
);
Expand Down
10 changes: 7 additions & 3 deletions Libraries/Lists/VirtualizedList_EXPERIMENTAL.js
Original file line number Diff line number Diff line change
Expand Up @@ -1754,13 +1754,17 @@ class VirtualizedList extends React.PureComponent<Props, State> {
});
};

_createViewToken = (index: number, isViewable: boolean) => {
const {data, getItem} = this.props;
_createViewToken = (
index: number,
isViewable: boolean,
props: FrameMetricProps,
) => {
const {data, getItem} = props;
const item = getItem(data, index);
return {
index,
item,
key: this._keyExtractor(item, index, this.props),
key: this._keyExtractor(item, index, props),
isViewable,
};
};
Expand Down

0 comments on commit a53512f

Please sign in to comment.