Skip to content

Commit

Permalink
refactor(useMap/useSet): refactoring useMap and useSet for improved i…
Browse files Browse the repository at this point in the history
…nitialValue 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
  • Loading branch information
KangXinzhi authored Mar 23, 2023
1 parent b3fd1bb commit b72366e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
28 changes: 27 additions & 1 deletion packages/hooks/src/useMap/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ describe('useMap', () => {

it('should init empty map if not initial object provided', () => {
const { result } = setup();

expect([...result.current[0]]).toEqual([]);

const { result: result2 } = setup(undefined);
expect([...result2.current[0]]).toEqual([]);
});

it('should get corresponding value for initial provided key', () => {
Expand Down Expand Up @@ -131,6 +133,12 @@ describe('useMap', () => {
['foo', 'foo'],
['a', 2],
]);

act(() => {
// @ts-ignore
utils.setAll();
});
expect([...result.current[0]]).toEqual([]);
});

it('remove should be work', () => {
Expand All @@ -141,6 +149,24 @@ describe('useMap', () => {
remove('msg');
});
expect(result.current[0].size).toBe(0);

const { result: result2 } = setup([
['foo', 'bar'],
['a', 1],
['b', 2],
['c', 3],
]);
const [, utils] = result2.current;

act(() => {
utils.remove('a');
});

expect([...result2.current[0]]).toEqual([
['foo', 'bar'],
['b', 2],
['c', 3],
]);
});

it('reset should be work', () => {
Expand Down
7 changes: 2 additions & 5 deletions packages/hooks/src/useMap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { useState } from 'react';
import useMemoizedFn from '../useMemoizedFn';

function useMap<K, T>(initialValue?: Iterable<readonly [K, T]>) {
const getInitValue = () => {
return initialValue === undefined ? new Map() : new Map(initialValue);
};

const [map, setMap] = useState<Map<K, T>>(() => getInitValue());
const getInitValue = () => new Map(initialValue);
const [map, setMap] = useState<Map<K, T>>(getInitValue);

const set = (key: K, entry: T) => {
setMap((prev) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/hooks/src/useSet/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ describe('useSet', () => {

it('should init empty set if no initial set provided', () => {
const { result } = setUp();

expect(result.current[0]).toEqual(new Set());

const { result: result1 } = setUp(undefined);
expect(result1.current[0]).toEqual(new Set());
});

it('should have an initially provided key', () => {
Expand Down
7 changes: 2 additions & 5 deletions packages/hooks/src/useSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { useState } from 'react';
import useMemoizedFn from '../useMemoizedFn';

function useSet<K>(initialValue?: Iterable<K>) {
const getInitValue = () => {
return initialValue === undefined ? new Set<K>() : new Set(initialValue);
};

const [set, setSet] = useState<Set<K>>(() => getInitValue());
const getInitValue = () => new Set(initialValue);
const [set, setSet] = useState<Set<K>>(getInitValue);

const add = (key: K) => {
if (set.has(key)) {
Expand Down

0 comments on commit b72366e

Please sign in to comment.