Reputation: 101
Setting this.setState inside a for loop skips after the first iteration. There is a switch statement inside for loop but it doesn't traverse all items.
eg:-
var items=[{key:"test",val:"hi"},{key:"test1",val:"hello"}];
for(let item in items){
console.log(items[item].key);
console.log(items[item]["key"]);
switch(items[item].key){
case "test" : {
console.log(items[item].val);
this.setState({testvar:items[item].val});
break;
}
case "test1" : {
console.log(items[item].val);
this.setState({test1var:items[item].val});
break;
}
}
}
Upvotes: 0
Views: 269
Reputation: 2056
Calling this.setState
will trigger a re-render of your component and a garbage clean-up of your for loop. You need to track all of the changes and call setState
once.
Upvotes: 1