Answer the question
In order to leave comments, you need to log in
How do I update the value of an object?
Hello. I have such an array in the state:
[{
"companyName": {
"key": "companyName",
"value": ""
},
"country": {
"key": "country",
"options": [],
"value": ""
},
"region": {
"key": "region",
"options": [],
"value": ""
},
"companyWebsite": {
"key": "companyWebsite",
"value": ""
}
}]
Answer the question
In order to leave comments, you need to log in
I don’t know React at all, but I see that you are constantly pushing a new object into the array, instead of changing the current one. Maybe that's what you wanted to do?
const handleChange = (index, value, key) => {
const newFields = [...fields];
newFields[index][key].value = value;
setFields(newFields);
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question