【问题标题】:react native dynamic setstate with string and dynamic index用字符串和动态索引反应原生动态 setstate
【发布时间】:2018-07-04 08:53:49
【问题描述】:

react native 动态生成 key1-100 状态。字符串和动态索引的组合。但是,如果没有 sytaz 错误,我怎么能做到这一点?

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

【问题讨论】:

  • 你应该更新 setState 之外的 key[index] 并替换 setSate 中的 key
  • 对不起,我不明白...

标签: react-native setstate


【解决方案1】:

要让 React Native 检测到状态的变化并因此重新渲染您的视图,您必须以 不可变 的方式调用 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);

【讨论】:

  • 非常感谢。真不知道可以这样写。
猜你喜欢
  • 2020-04-20
  • 2018-03-28
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 2017-01-13
相关资源
最近更新 更多