Skip to content

Commit 3fe01eb

Browse files
committed
Improve ListBox performance by memoizing dependencies that otherwise change every render while the hover/selection state changes.
1 parent 3c9b36a commit 3fe01eb

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

packages/@react-stately/list/src/useListState.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,25 @@ export function useListState<T extends object>(props: ListProps<T>): ListState<T
6363

6464
useFocusedKeyReset(collection, selectionManager);
6565

66-
return {
66+
return useMemo(() => ({
6767
collection,
6868
disabledKeys,
6969
selectionManager
70-
};
70+
}), [collection, disabledKeys, selectionManager]);
7171
}
7272

7373
/**
7474
* Filters a collection using the provided filter function and returns a new ListState.
7575
*/
7676
export function UNSTABLE_useFilteredListState<T extends object>(state: ListState<T>, filter: ((nodeValue: string) => boolean) | null | undefined): ListState<T> {
7777
let collection = useMemo(() => filter ? state.collection.UNSTABLE_filter!(filter) : state.collection, [state.collection, filter]);
78-
let selectionManager = state.selectionManager.withCollection(collection);
78+
let selectionManager = useMemo(() => state.selectionManager.withCollection(collection), [collection]);
7979
useFocusedKeyReset(collection, selectionManager);
80-
return {
80+
return useMemo(() => ({
8181
collection,
8282
selectionManager,
8383
disabledKeys: state.disabledKeys
84-
};
84+
}), [collection, selectionManager, state.disabledKeys]);
8585
}
8686

8787
function useFocusedKeyReset<T>(collection: Collection<Node<T>>, selectionManager: SelectionManager) {

packages/react-aria-components/src/ListBox.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ function ListBoxInner<T extends object>({state: inputState, props, listBoxRef}:
249249
values={[
250250
[ListBoxContext, props],
251251
[ListStateContext, state],
252-
[DragAndDropContext, {dragAndDropHooks, dragState, dropState}],
252+
[DragAndDropContext, useMemo(() => ({dragAndDropHooks, dragState, dropState}), [dragAndDropHooks, dragState, dropState])],
253253
[SeparatorContext, {elementType: 'div'}],
254254
[DropIndicatorContext, {render: ListBoxDropIndicatorWrapper}],
255255
[SectionContext, {name: 'ListBoxSection', render: ListBoxSectionInner}]

0 commit comments

Comments
 (0)