Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(useMap/useSet): refactoring useMap and useSet for improved initialValue checking. #2116

Merged
merged 4 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
11 changes: 4 additions & 7 deletions packages/hooks/src/useMap/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { useState } from 'react';
import { useMemo, 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 = useMemo(() => new Map(initialValue), [initialValue]);
const [map, setMap] = useState<Map<K, T>>(getInitValue);
liuyib marked this conversation as resolved.
Show resolved Hide resolved

const set = (key: K, entry: T) => {
setMap((prev) => {
Expand All @@ -28,7 +25,7 @@ function useMap<K, T>(initialValue?: Iterable<readonly [K, T]>) {
});
};

const reset = () => setMap(getInitValue());
const reset = () => setMap(getInitValue);

const get = (key: K) => map.get(key);

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
11 changes: 4 additions & 7 deletions packages/hooks/src/useSet/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { useState } from 'react';
import { useMemo, 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 = useMemo(() => new Set(initialValue), [initialValue]);
const [set, setSet] = useState<Set<K>>(getInitValue);
liuyib marked this conversation as resolved.
Show resolved Hide resolved
li-jia-nan marked this conversation as resolved.
Show resolved Hide resolved

const add = (key: K) => {
if (set.has(key)) {
Expand All @@ -30,7 +27,7 @@ function useSet<K>(initialValue?: Iterable<K>) {
});
};

const reset = () => setSet(getInitValue());
const reset = () => setSet(getInitValue);

return [
set,
Expand Down