Skip to content

Commit f57a9d1

Browse files
committed
perf: optimize FlatList props
1 parent f4fa5a6 commit f57a9d1

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

package/src/components/MessageList/MessageList.tsx

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@ const MessageListWithContext = <
362362

363363
const [autoscrollToRecent, setAutoscrollToRecent] = useState(false);
364364

365+
const maintainVisibleContentPosition = useMemo(
366+
() => ({
367+
autoscrollToTopThreshold: autoscrollToRecent ? 10 : undefined,
368+
minIndexForVisible: 1,
369+
}),
370+
[autoscrollToRecent],
371+
);
372+
365373
/**
366374
* We want to call onEndReached and onStartReached only once, per content length.
367375
* We keep track of calls to these functions per content length, with following trackers.
@@ -1183,6 +1191,25 @@ const MessageListWithContext = <
11831191
additionalFlatListPropsExcludingStyle = rest;
11841192
}
11851193

1194+
const flatListStyle = useMemo(
1195+
() => [
1196+
styles.listContainer,
1197+
listContainer,
1198+
additionalFlatListProps?.style,
1199+
shouldApplyAndroidWorkaround ? styles.invertAndroid : undefined,
1200+
],
1201+
[additionalFlatListProps?.style, listContainer, shouldApplyAndroidWorkaround],
1202+
);
1203+
1204+
const flatListContentContainerStyle = useMemo(
1205+
() => [
1206+
styles.contentContainer,
1207+
additionalFlatListProps?.contentContainerStyle,
1208+
contentContainer,
1209+
],
1210+
[additionalFlatListProps?.contentContainerStyle, contentContainer],
1211+
);
1212+
11861213
if (!FlatList) {
11871214
return null;
11881215
}
@@ -1207,11 +1234,7 @@ const MessageListWithContext = <
12071234
</View>
12081235
) : (
12091236
<FlatList
1210-
contentContainerStyle={[
1211-
styles.contentContainer,
1212-
additionalFlatListProps?.contentContainerStyle,
1213-
contentContainer,
1214-
]}
1237+
contentContainerStyle={flatListContentContainerStyle}
12151238
/** Disables the MessageList UI. Which means, message actions, reactions won't work. */
12161239
data={processedMessageList}
12171240
extraData={disabled}
@@ -1227,10 +1250,7 @@ const MessageListWithContext = <
12271250
minIndexForVisible = 1 means that beyond the item at index 1 we will not change the position on list updates,
12281251
however it is not used when autoscrollToTopThreshold = 10.
12291252
*/
1230-
maintainVisibleContentPosition={{
1231-
autoscrollToTopThreshold: autoscrollToRecent ? 10 : undefined,
1232-
minIndexForVisible: 1,
1233-
}}
1253+
maintainVisibleContentPosition={maintainVisibleContentPosition}
12341254
maxToRenderPerBatch={30}
12351255
onMomentumScrollEnd={onUserScrollEvent}
12361256
onScroll={handleScroll}
@@ -1243,12 +1263,7 @@ const MessageListWithContext = <
12431263
renderItem={renderItem}
12441264
scrollEnabled={overlay === 'none'}
12451265
showsVerticalScrollIndicator={!shouldApplyAndroidWorkaround}
1246-
style={[
1247-
styles.listContainer,
1248-
listContainer,
1249-
additionalFlatListProps?.style,
1250-
shouldApplyAndroidWorkaround ? styles.invertAndroid : undefined,
1251-
]}
1266+
style={flatListStyle}
12521267
testID='message-flat-list'
12531268
viewabilityConfig={flatListViewabilityConfig}
12541269
{...additionalFlatListPropsExcludingStyle}

0 commit comments

Comments
 (0)