11# useMap
2- Hooks to use _ Map data structure_ to handle component state.
2+ Hooks to use _ Map data structure_ to handle component state with all Map methods .
33
44## Usage
55
66``` tsx
7-
87const UseMap = () => {
98 const map = useMap <string , number >();
109
@@ -27,14 +26,21 @@ const UseMap = () => {
2726 map .forEach ((value , key , map ) => { map .set (key , value + 1 ) });
2827 }, [map ]);
2928
29+ const parsed = useMemo (() => {
30+ const temp: string [] = [];
31+ temp .map
32+ map .forEach ((value , key ) => temp .push (key + " - " + value ));
33+ return temp .join (" , " );
34+ }, [map ]);
35+
3036 return (<>
31- { map . map (( value , key ) => key + " - " + value ). join ( " , " ) }
37+ { parsed }
3238 <br />
3339 <input ref = { inputRef } type = " text" />
3440 <br />
3541 <div style = { { marginTop: 15 , gridTemplateColumns: ' auto auto' , justifyContent: ' center' , display: ' grid' , gap: ' 5px' }} >
36- <button onClick = { del } >Delete</button >
3742 <button onClick = { set } >Set</button >
43+ <button onClick = { del } >Delete</button >
3844 <button onClick = { clear } >Clear</button >
3945 <button onClick = { incrementAll } >Increment all</button >
4046 </div >
@@ -46,7 +52,13 @@ UseMap.displayName = "UseMap";
4652export {UseMap }
4753```
4854
49-
55+ > The component has:
56+ > - A _ useMap_ internal state.
57+ > - An uncontrolled input with _ inputRef_ ref used to execute buttons actions.
58+ > - A button _ Set_ that set the input value (format like __ id__ _ -_ __ value__ ) into state by _ set_ method of Map interface.
59+ > - A button _ Delete_ that remove item into state by _ key_ , written in input by _ delete_ method of Map interface.
60+ > - A button _ Clear_ that clear state by _ clear_ method of Map interface.
61+ > - A button _ IncrementAll_ that adds 1 to every value into map by _ forEach_ method of Map interface.
5062
5163
5264## API
@@ -64,6 +76,5 @@ An Array or other iterable object whose elements are key-value pairs, or functio
6476> ### Returns
6577>
6678>
67- > - _ Map<K_
68- > - _ V>_
79+ > - _ Map<K,V>_
6980>
0 commit comments