How do I update the value of an object?
-
Hey. 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": "" } }]
I have a text input, onChange of which the value in this array should change. There can be many objects, so indexing is needed here.
https://codesandbox.io/s/funny -euclid-hyhi5? fontsi ...
Can you tell me what I'm doing wrong ??JavaScript Lukas Zhang, Aug 3, 2020 -
I don't know the react at all, but I see that you are constantly pushing a new object into the array, instead of changing the current one. Is that what you wanted to do?
const handleChange = (index, value, key) => {
const newFields = [...fields];
newFields[index][key].value = value;
setFields(newFields);
};Anonymous
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!