Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  89] [ 5]  / answers: 1 / hits: 5501  / 4 Years ago, sat, july 18, 2020, 12:00:00

I'm trying to update my state (an array of objects), but get the above error whenever I try to use .map or edit a clone of the state object.


  React.useEffect(() => {
setUserMeasurements((oldUserMeasurements) => {
return oldUserMeasurements.map(nameAndMeasure => {
if (nameAndMeasure.name === name) { nameAndMeasure.measure = 60 }
return nameAndMeasure;
})
})

})


It doesn't seem to like it when I try the "nameAndMeasure.measure = 60" section of code, but I can't understand why. Can anyone explain?


More From » reactjs

 Answers
4

I found that I could use this function to do what I was trying to get .map to do:


function replaceItemAtIndex(arr, index, newValue) {  return [...arr.slice(0, index), newValue, ...arr.slice(index + 1)]; }

The .slice methods seem to create a new array, which satisfies javascript in a way that .map doesn't.


[#3137] Wednesday, July 15, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
Thu, May 16, 19, 00:00, 5 Years ago
;