用字符串和动态索引反应本地动态setstate

时间:2018-07-04 08:53:50

标签: react-native setstate

react native动态生成key1-100状态。字符串和动态索引的组合。但是,我该怎么做却没有sytaz错误?

this.setstate({ 
     key[index]:1 
})

1 个答案:

答案 0 :(得分:0)

要使React Native能够检测到状态更改并因此重新呈现视图,必须以 immutable 的方式调用setState。因此,请尝试以下操作:

//copy state
const newState = {...this.state};
//access property - "key1, key2, etc" whatever index is.
newState['key'+index] = 1;
// set new state
this.setState(newState);