Skip to content

Commit b72366e

Browse files
authored
refactor(useMap/useSet): refactoring useMap and useSet for improved initialValue checking. (#2116)
* fix(useMap): map initialValue supports undefined * fix(useSet): set initialValue supports undefined * fix: use memoized state in useMap and useSet * fix: getInitValue is used as a function
1 parent b3fd1bb commit b72366e

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

packages/hooks/src/useMap/__tests__/index.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ describe('useMap', () => {
2626

2727
it('should init empty map if not initial object provided', () => {
2828
const { result } = setup();
29-
3029
expect([...result.current[0]]).toEqual([]);
30+
31+
const { result: result2 } = setup(undefined);
32+
expect([...result2.current[0]]).toEqual([]);
3133
});
3234

3335
it('should get corresponding value for initial provided key', () => {
@@ -131,6 +133,12 @@ describe('useMap', () => {
131133
['foo', 'foo'],
132134
['a', 2],
133135
]);
136+
137+
act(() => {
138+
// @ts-ignore
139+
utils.setAll();
140+
});
141+
expect([...result.current[0]]).toEqual([]);
134142
});
135143

136144
it('remove should be work', () => {
@@ -141,6 +149,24 @@ describe('useMap', () => {
141149
remove('msg');
142150
});
143151
expect(result.current[0].size).toBe(0);
152+
153+
const { result: result2 } = setup([
154+
['foo', 'bar'],
155+
['a', 1],
156+
['b', 2],
157+
['c', 3],
158+
]);
159+
const [, utils] = result2.current;
160+
161+
act(() => {
162+
utils.remove('a');
163+
});
164+
165+
expect([...result2.current[0]]).toEqual([
166+
['foo', 'bar'],
167+
['b', 2],
168+
['c', 3],
169+
]);
144170
});
145171

146172
it('reset should be work', () => {

packages/hooks/src/useMap/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ import { useState } from 'react';
22
import useMemoizedFn from '../useMemoizedFn';
33

44
function useMap<K, T>(initialValue?: Iterable<readonly [K, T]>) {
5-
const getInitValue = () => {
6-
return initialValue === undefined ? new Map() : new Map(initialValue);
7-
};
8-
9-
const [map, setMap] = useState<Map<K, T>>(() => getInitValue());
5+
const getInitValue = () => new Map(initialValue);
6+
const [map, setMap] = useState<Map<K, T>>(getInitValue);
107

118
const set = (key: K, entry: T) => {
129
setMap((prev) => {

packages/hooks/src/useSet/__tests__/index.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ describe('useSet', () => {
1818

1919
it('should init empty set if no initial set provided', () => {
2020
const { result } = setUp();
21-
2221
expect(result.current[0]).toEqual(new Set());
22+
23+
const { result: result1 } = setUp(undefined);
24+
expect(result1.current[0]).toEqual(new Set());
2325
});
2426

2527
it('should have an initially provided key', () => {

packages/hooks/src/useSet/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ import { useState } from 'react';
22
import useMemoizedFn from '../useMemoizedFn';
33

44
function useSet<K>(initialValue?: Iterable<K>) {
5-
const getInitValue = () => {
6-
return initialValue === undefined ? new Set<K>() : new Set(initialValue);
7-
};
8-
9-
const [set, setSet] = useState<Set<K>>(() => getInitValue());
5+
const getInitValue = () => new Set(initialValue);
6+
const [set, setSet] = useState<Set<K>>(getInitValue);
107

118
const add = (key: K) => {
129
if (set.has(key)) {

0 commit comments

Comments
 (0)