Skip to content

Commit

Permalink
Fix CellRenderer onCellFocusCapture not being stable (#41381)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #41381

Noticed that when scrolling VirtualizedList's CellRenderer was re-rendering due to `onCellFocusCapture` not having a stable identify. Change the interface to CellRenderer to pass in the `cellKey` in the callback to save on creating new callbacks.

Changelog: [Internal]

Reviewed By: sammy-SC

Differential Revision: D51112928

fbshipit-source-id: 3fcb974d9b5585403895746fbc45f2cf5a9fa6b1
  • Loading branch information
javache authored and facebook-github-bot committed Nov 8, 2023
1 parent ae9302c commit 050a5a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/virtualized-lists/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
key={key}
prevCellKey={prevCellKey}
onUpdateSeparators={this._onUpdateSeparators}
onCellFocusCapture={e => this._onCellFocusCapture(key)}
onCellFocusCapture={this._onCellFocusCapture}
onUnmount={this._onCellUnmount}
ref={ref => {
this._cellRefs[key] = ref;
Expand Down Expand Up @@ -1314,10 +1314,10 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
this._updateViewableItems(this.props, this.state.cellsAroundViewport);
};

_onCellFocusCapture(cellKey: string) {
_onCellFocusCapture = (cellKey: string) => {
this._lastFocusedCellKey = cellKey;
this._updateCellsToRender();
}
};

_onCellUnmount = (cellKey: string) => {
delete this._cellRefs[cellKey];
Expand Down
22 changes: 12 additions & 10 deletions packages/virtualized-lists/Lists/VirtualizedListCellRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type Props<ItemT> = {
inversionStyle: ViewStyleProp,
item: ItemT,
onCellLayout?: (event: LayoutEvent, cellKey: string, index: number) => void,
onCellFocusCapture?: (event: FocusEvent) => void,
onCellFocusCapture?: (cellKey: string) => void,
onUnmount: (cellKey: string) => void,
onUpdateSeparators: (
cellKeys: Array<?string>,
Expand Down Expand Up @@ -115,12 +115,15 @@ export default class CellRenderer<ItemT> extends React.Component<
}

_onLayout = (nativeEvent: LayoutEvent): void => {
this.props.onCellLayout &&
this.props.onCellLayout(
nativeEvent,
this.props.cellKey,
this.props.index,
);
this.props.onCellLayout?.(
nativeEvent,
this.props.cellKey,
this.props.index,
);
};

_onCellFocusCapture = (e: FocusEvent): void => {
this.props.onCellFocusCapture?.(this.props.cellKey);
};

_renderElement(
Expand Down Expand Up @@ -174,7 +177,6 @@ export default class CellRenderer<ItemT> extends React.Component<
item,
index,
inversionStyle,
onCellFocusCapture,
onCellLayout,
renderItem,
} = this.props;
Expand Down Expand Up @@ -206,7 +208,7 @@ export default class CellRenderer<ItemT> extends React.Component<
const result = !CellRendererComponent ? (
<View
style={cellStyle}
onFocusCapture={onCellFocusCapture}
onFocusCapture={this._onCellFocusCapture}
{...(onCellLayout && {onLayout: this._onLayout})}>
{element}
{itemSeparator}
Expand All @@ -217,7 +219,7 @@ export default class CellRenderer<ItemT> extends React.Component<
index={index}
item={item}
style={cellStyle}
onFocusCapture={onCellFocusCapture}
onFocusCapture={this._onCellFocusCapture}
{...(onCellLayout && {onLayout: this._onLayout})}>
{element}
{itemSeparator}
Expand Down

0 comments on commit 050a5a3

Please sign in to comment.