I'm new to reactjs and that's why it comes naive to you.
I need to update the value of a map in which its keys are unknown.
const App = () => {
const [storeMap, setStoreMap] = useState(new Map());
let _tmpMap = new Map();
return (<>
{Object.keys({ key1: "hey", key2: "you" }).map((item) => {
return (
<button
value={item}
key={item}
onClick={(e) => {
_tmpMap.set(item, e.target.value);
console.log(..._tmpMap); // {1}
setStoreMap(_tmpMap);
}}
>
{item}
</button>
);
// return <i key={item}>KJ </i>;
})}
</>)
}
What I am expecting to see in the above code after clicking both buttons is:
/* {1} */
console.log(..._tmpMap)
//i expect this: {key1:"key1" , key2:"key2"}
What I see in reality is {key1:"key1"}
after pressing key 1 and { key2:"key2"}
after pressing key 2
My question is:
How can I update storeMap while preserving its previous entries?