You can create an object before calling setState .
var newState = {}; newState[i] = number; _this.setState(newState);
Alternatively, if you are using ES6, you can use the computed property .
_this.setState({ [i]: number });
However, this code will call setState several times, it is more efficient to call it only once. Create an updated state object, then apply it.
var newState = {}; for(var i in arr) { newState[i] = number; } this.setState(newState);
Dan prince
source share