Fix initialisation error

Summary: There was an initialisation error possible if the crash reporter tried to start with an initial selection (after coming from a deeplink or the notification pane), which would cause an unending update loop.

Reviewed By: passy

Differential Revision: D29331127

fbshipit-source-id: 14e75e483c232039e6a80aa131fa5ce7c105b670
This commit is contained in:
Michel Weststrate
2021-06-24 01:35:55 -07:00
committed by Facebook GitHub Bot
parent db787d5d42
commit 2127dea447
2 changed files with 10 additions and 12 deletions

View File

@@ -357,12 +357,16 @@ export function DataTable<T extends object>(
[dataSource, tableState.sorting],
);
const isMounted = useRef(false);
useEffect(
function triggerSelection() {
onSelect?.(
getSelectedItem(dataSource, tableState.selection),
getSelectedItems(dataSource, tableState.selection),
);
if (isMounted.current) {
onSelect?.(
getSelectedItem(dataSource, tableState.selection),
getSelectedItems(dataSource, tableState.selection),
);
}
isMounted.current = true;
},
[onSelect, dataSource, tableState.selection],
);

View File

@@ -555,10 +555,7 @@ test('onSelect callback fires, and in order', () => {
ref.current!.selectItem(2);
});
expect(events.splice(0)).toEqual([
[undefined, []],
[item3, [item3]],
]);
expect(events.splice(0)).toEqual([[item3, [item3]]]);
act(() => {
ref.current!.addRangeToSelection(0, 0);
@@ -606,10 +603,7 @@ test('selection always has the latest state', () => {
ref.current!.selectItem(2);
});
expect(events.splice(0)).toEqual([
[undefined, []],
[item3, [item3]],
]);
expect(events.splice(0)).toEqual([[item3, [item3]]]);
const item3updated = {
title: 'item 3 updated',