I have a form in React JS with some data that will have dynamic keys so I don't want to hardcode their assignment in setData. (Context: I want the form to contain these values when the form loads)
Basically, I have a map which maps all of my keys to their values. I am trying to do something along the lines of:
// Keys and values held here.
myMap = Map<string, mixed>;
const [data, setData] = useState(() => {
const data = {
submit: 0,
// Set data that I know the keys of here.
};
// Set data that I don't know the keys of here.
// I am trying to dynamically set data[key] as follows (which does not work):
Object.keys(myMap).forEach(key => data[key] = myMap[key]);
return data;
});
Anyone know why this is not working / how I can accomplish this?