Memoize VirtualizedListCellContextProvider

Summary:
When a FlatList is nested inside another FlatList, it may be re-rendered whenever the outer FlatList renders. Apply the same optimization we already had in `VirtualizedListContextProvider` to avoid changing the context object if no values have changed.

Changelog: [General][Changed] - Optimized VirtualizedList context when used with nested lists

Reviewed By: genkikondo

Differential Revision: D35905952

fbshipit-source-id: 695253c85db2043d22e208ad94ecc7daa1455055
This commit is contained in:
Pieter De Baets
2022-04-26 04:04:45 -07:00
committed by Facebook GitHub Bot
parent f40976cd24
commit ceb0a54608
+7 -3
View File
@@ -142,10 +142,14 @@ export function VirtualizedListCellContextProvider({
cellKey: string,
children: React.Node,
}): React.Node {
const context = useContext(VirtualizedListContext);
// Avoid setting a newly created context object if the values are identical.
const currContext = useContext(VirtualizedListContext);
const context = useMemo(
() => (currContext == null ? null : {...currContext, cellKey}),
[currContext, cellKey],
);
return (
<VirtualizedListContext.Provider
value={context == null ? null : {...context, cellKey}}>
<VirtualizedListContext.Provider value={context}>
{children}
</VirtualizedListContext.Provider>
);