You should you setState with a function so you don't change state directly.
this.setState(prevState => {
for(let k in prevState.content.text){
prevState.content.text[k].line = "changed";
}
return {content: prevState.content}
}
Edit:
I'm not sure if changing prevState directly is a good thing (please some one correct me), but you can also do
this.setState(prevState => {
let changedState = {...prevState}
for(let k in changedState.content.text){
changedState.content.text[k].line = "changed";
}
return {content: changedState.content}
}
Edit:
As said in the comments, {...prevState} is going to be a shallow copy and it can still change the state directly. One solution to this is use lodash cloneDeep
.reduce()to do that?.reduceis not suitable for that purpose for a whole lot of reasons, where the first one is that it's goal is to reduce an initial item and meant to work on arrays, while you need to alter an object instead. Just use a regular for..in or.forEachinstead on the object keys.