From 488a4c7e1c86ac5900ff9194106511fbf5a8e3cb Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Fri, 25 May 2018 14:59:24 -0700 Subject: [PATCH] Fix VirtualizedSectionList:ItemWithSeparators Reviewed By: yungsters Differential Revision: D8021407 fbshipit-source-id: 480547d867eda476fe6ddf4af74072ad1851a427 --- Libraries/Lists/VirtualizedSectionList.js | 48 ++++++++++++++++------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/Libraries/Lists/VirtualizedSectionList.js b/Libraries/Lists/VirtualizedSectionList.js index 6da08dc1ab6659..cfedc61b0919d3 100644 --- a/Libraries/Lists/VirtualizedSectionList.js +++ b/Libraries/Lists/VirtualizedSectionList.js @@ -378,25 +378,40 @@ class VirtualizedSectionList extends React.PureComponent< }; } -type ItemWithSeparatorProps = { - LeadingSeparatorComponent: ?React.ComponentType<*>, - SeparatorComponent: ?React.ComponentType<*>, +type ItemWithSeparatorCommonProps = $ReadOnly<{| + leadingItem: ?Item, + leadingSection: ?Object, + section: Object, + trailingItem: ?Item, + trailingSection: ?Object, +|}>; + +type ItemWithSeparatorProps = $ReadOnly<{| + ...ItemWithSeparatorCommonProps, + LeadingSeparatorComponent: ?React.ComponentType, + SeparatorComponent: ?React.ComponentType, cellKey: string, index: number, item: Item, onUpdateSeparator: (cellKey: string, newProps: Object) => void, prevCellKey?: ?string, renderItem: Function, - section: Object, - leadingItem: ?Item, - leadingSection: ?Object, - trailingItem: ?Item, - trailingSection: ?Object, +|}>; + +type ItemWithSeparatorState = { + separatorProps: $ReadOnly<{| + highlighted: false, + ...ItemWithSeparatorCommonProps, + |}>, + leadingSeparatorProps: $ReadOnly<{| + highlighted: false, + ...ItemWithSeparatorCommonProps, + |}>, }; class ItemWithSeparator extends React.Component< ItemWithSeparatorProps, - $FlowFixMeState, + ItemWithSeparatorState, > { state = { separatorProps: { @@ -430,7 +445,7 @@ class ItemWithSeparator extends React.Component< }, updateProps: (select: 'leading' | 'trailing', newProps: Object) => { const {LeadingSeparatorComponent, cellKey, prevCellKey} = this.props; - if (select === 'leading' && LeadingSeparatorComponent) { + if (select === 'leading' && LeadingSeparatorComponent != null) { this.setState(state => ({ leadingSeparatorProps: {...state.leadingSeparatorProps, ...newProps}, })); @@ -443,10 +458,13 @@ class ItemWithSeparator extends React.Component< }, }; - UNSAFE_componentWillReceiveProps(props: ItemWithSeparatorProps) { - this.setState(state => ({ + static getDerivedStateFromProps( + props: ItemWithSeparatorProps, + prevState: ItemWithSeparatorState, + ): ?ItemWithSeparatorState { + return { separatorProps: { - ...this.state.separatorProps, + ...prevState.separatorProps, leadingItem: props.item, leadingSection: props.leadingSection, section: props.section, @@ -454,14 +472,14 @@ class ItemWithSeparator extends React.Component< trailingSection: props.trailingSection, }, leadingSeparatorProps: { - ...this.state.leadingSeparatorProps, + ...prevState.leadingSeparatorProps, leadingItem: props.leadingItem, leadingSection: props.leadingSection, section: props.section, trailingItem: props.item, trailingSection: props.trailingSection, }, - })); + }; } updateSeparatorProps(newProps: Object) {