diff --git a/packages/hooks/src/useSelections/__tests__/index.test.ts b/packages/hooks/src/useSelections/__tests__/index.test.ts index 15cfa76191..07088485f0 100644 --- a/packages/hooks/src/useSelections/__tests__/index.test.ts +++ b/packages/hooks/src/useSelections/__tests__/index.test.ts @@ -183,6 +183,12 @@ describe('useSelections', () => { expect(result.current.noneSelected).toBe(false); expect(result.current.allSelected).toBe(true); expect(result.current.partiallySelected).toBe(false); + + // Keep compatible with older versions. + act(() => { + expect(() => setSelected(undefined!)).not.toThrowError(); + expect(() => setSelected(null!)).not.toThrowError(); + }); }; runCaseCallback(caseCallback, caseCallback); diff --git a/packages/hooks/src/useSelections/index.ts b/packages/hooks/src/useSelections/index.ts index e98ce0fa3b..a8ddc2056e 100644 --- a/packages/hooks/src/useSelections/index.ts +++ b/packages/hooks/src/useSelections/index.ts @@ -36,6 +36,10 @@ export default function useSelections(items: T[], options?: T[] | Options) const selectedMap = useMemo(() => { const keyToItemMap = new Map(); + if (!Array.isArray(selected)) { + return keyToItemMap; + } + selected.forEach((item) => { keyToItemMap.set(getKey(item), item); });