- React: Setting state to an es6 Map: 如何在 state 中使用 Map
- How does shallow compare work in react ```typescript import { useCallback, useState } from ‘react’;
const useMap =
const set = useCallback( (key: K, value: V) => { setMap((prev) => { const t = new Map(prev); t.set(key, value); return t; }); }, [setMap], );
const has = useCallback((key: K) => map.has(key), [map]);
const get = useCallback((key: K) => map.get(key), [map]);
const remove = useCallback( (key: K) => { setMap((prev) => { const t = new Map(prev); t.delete(key); return t; }); }, [map], );
const toArray = useCallback(() => { return Array.from(map); }, [map]);
const toObject = useCallback(() => { return Object.fromEntries(map); }, [map]);
return [map, { set, has, get, remove, toArray, toObject }] as const; };
export default useMap;
```