MCPcopy Create free account
hub / github.com/streamich/react-use / useMap

Function useMap

src/useMap.ts:14–45  ·  view source on GitHub ↗
(initialMap: T = {} as T)

Source from the content-addressed store, hash-verified

12}
13
14const useMap = <T extends object = any>(initialMap: T = {} as T): [T, Actions<T>] => {
15 const [map, set] = useState<T>(initialMap);
16
17 const stableActions = useMemo<StableActions<T>>(
18 () => ({
19 set: (key, entry) => {
20 set((prevMap) => ({
21 ...prevMap,
22 [key]: entry,
23 }));
24 },
25 setAll: (newMap: T) => {
26 set(newMap);
27 },
28 remove: (key) => {
29 set((prevMap) => {
30 const { [key]: omit, ...rest } = prevMap;
31 return rest as T;
32 });
33 },
34 reset: () => set(initialMap),
35 }),
36 [set]
37 );
38
39 const utils = {
40 get: useCallback((key) => map[key], [map]),
41 ...stableActions,
42 } as Actions<T>;
43
44 return [map, utils];
45};
46
47export default useMap;

Callers 2

DemoFunction · 0.90
setUpFunction · 0.85

Calls 1

setFunction · 0.70

Tested by 1

setUpFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…