BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/guarahooks/use-map

UseMap

guarahooks/use-map
FreeHook

Manages a Map of key/value pairs with ease.

Live preview

live · rendered by the registry
Rendered by guarahooks on their own page — this is the component running, not a screenshot of it.

Install it

npx shadcn@latest add https://guarahooks.com/r/use-map.json

Source

registry/hooks/use-map.tsxguarahooks.com/r/use-map.json
1'use client';
2
3import { useCallback, useMemo, useState } from 'react';
4
5export type UseMapResult<K, V> = {
6 map: Map<K, V>;
7 set: (key: K, value: V) => void;
8 delete: (key: K) => void;
9 has: (key: K) => boolean;
10 clear: () => void;
11 reset: () => void;
12 setValue: (newMap: Map<K, V>) => void;
13 get: (key: K) => V | undefined;
14 getAll: () => [K, V][];
15};
16
17export function useMap<K, V>(
18 initialMap: Iterable<[K, V]> = [],
19): UseMapResult<K, V> {
20 const [map, setMap] = useState<Map<K, V>>(() => new Map(initialMap));
21 const initial = useMemo(() => new Map(initialMap), [initialMap]);
22
23 const setEntry = useCallback((key: K, value: V) => {
24 setMap((prev) => {
25 const current = prev.get(key);
26 if (prev.has(key) && current === value) return prev;
27 const next = new Map(prev);
28 next.set(key, value);
29 return next;
30 });
31 }, []);
32
33 const deleteEntry = useCallback((key: K) => {
34 setMap((prev) => {
35 if (!prev.has(key)) return prev;
36 const next = new Map(prev);
37 next.delete(key);
38 return next;
39 });
40 }, []);
41
42 const hasKey = useCallback((key: K) => map.has(key), [map]);
43
44 const clear = useCallback(() => {
45 setMap(new Map());
46 }, []);
47
48 const reset = useCallback(() => {
49 setMap(new Map(initial));
50 }, [initial]);
51
52 const setValue = useCallback((newMap: Map<K, V>) => {
53 setMap(new Map(newMap));
54 }, []);
55
56 const get = useCallback((key: K) => map.get(key), [map]);
57
58 const getAll = useCallback(() => Array.from(map.entries()), [map]);
59
60 return {
61 map,
62 set: setEntry,
63 delete: deleteEntry,
64 has: hasKey,
65 clear,
66 reset,
67 setValue,
68 get,
69 getAll,
70 };
71}

Files it writes

  • registry/hooks/use-map.tsx

Facts

Registry
guarahooks
Type
registry:hook
Access
Free
Files written
1
Licence
MIT
First indexed
2026-08-03
Last confirmed
yesterday

Go to the source

Docs on guarahooks guarahooks.com h3rmel/guarahooks on GitHub Raw registry item This item as JSON

More from guarahooks

All 100 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
UseAbortControlleruse-abort-controllerguarahooksHooksFreeno deps
UseArrayStateuse-array-stateguarahooksHooksFreeno deps
UseAxiosuse-axiosguarahooksHooksFreeno deps
UseBatteryStatususe-battery-statusguarahooksHooksFreeno deps
UseBetterAuthuse-better-authguarahooksHooksFreeno deps
UseClickOutsideuse-click-outsideguarahooksHooksFreeno deps
UseConfirmuse-confirmguarahooksHooksFreeno deps
UseCookieuse-cookieguarahooksHooksFreeno deps
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex