From 6469d4b52944c98a5aa9f3c62e4fcbc314e287fc Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 2 Aug 2022 01:30:21 -0700 Subject: [PATCH] VirtualizedList up-to-date state: Thread props to computeWindowedRenderLimits() 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 Use passed props in `computeWindowedRenderLimits()`, which is called during a state update. ## 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 computeWindowedRenderLimits() Reviewed By: genkikondo Differential Revision: D38293588 fbshipit-source-id: 1330a003c1354bbd63c0b7b33a013e85e96fdc64 --- Libraries/Lists/VirtualizeUtils.js | 7 +++---- Libraries/Lists/VirtualizedList.js | 3 +-- Libraries/Lists/VirtualizedList_EXPERIMENTAL.js | 3 +-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Libraries/Lists/VirtualizeUtils.js b/Libraries/Lists/VirtualizeUtils.js index c8ccb07cb30..9351505dcad 100644 --- a/Libraries/Lists/VirtualizeUtils.js +++ b/Libraries/Lists/VirtualizeUtils.js @@ -10,7 +10,7 @@ 'use strict'; -import invariant from 'invariant'; +import type {FrameMetricProps} from './VirtualizedListProps'; /** * Used to find the indices of the frames that overlap the given offsets. Useful for finding the @@ -95,8 +95,7 @@ export function newRangeCount( * biased in the direction of scroll. */ export function computeWindowedRenderLimits( - data: any, - getItemCount: (data: any) => number, + props: FrameMetricProps, maxToRenderPerBatch: number, windowSize: number, prev: { @@ -120,7 +119,7 @@ export function computeWindowedRenderLimits( first: number, last: number, } { - const itemCount = getItemCount(data); + const itemCount = props.getItemCount(props.data); if (itemCount === 0) { return prev; } diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index 539e6565e1d..a0dc214d798 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -1553,8 +1553,7 @@ class VirtualizedList extends React.PureComponent { Math.abs(distanceFromEnd) < Number.EPSILON ) { newState = computeWindowedRenderLimits( - this.props.data, - this.props.getItemCount, + this.props, maxToRenderPerBatchOrDefault(this.props.maxToRenderPerBatch), windowSizeOrDefault(this.props.windowSize), state, diff --git a/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js b/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js index 34d15c86880..89d972ef55c 100644 --- a/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js +++ b/Libraries/Lists/VirtualizedList_EXPERIMENTAL.js @@ -641,8 +641,7 @@ class VirtualizedList extends React.PureComponent { } newCellsAroundViewport = computeWindowedRenderLimits( - props.data, - props.getItemCount, + props, maxToRenderPerBatchOrDefault(props.maxToRenderPerBatch), windowSizeOrDefault(props.windowSize), cellsAroundViewport,