I'm getting an error that 'exampleState' is not iterable, but why?
Say that I have a state variable which holds a simple array of objects
const [exampleState, setExampleState] = useState([
{
id: 1,
name: 'John'
},
{
id: 2,
name: 'Mary'
}
])
I want to change the values of these objects conditionally. I thought a loop with a conditional inside would be an easy way to do this. I tried the following...
for (let item of exampleState) {
if (item.name === 'John') {
setExampleState({...exampleState, name: 'Michael'})
}
}
but I get the following error: 'TypeError' exampleState is not iterable
How should I do this instead?
for ... ininstead offor ... ofhere I think and the rest should just work.