【发布时间】:2019-03-08 12:28:28
【问题描述】:
对于问题的措辞令人困惑,我深表歉意。基本上当我从这里调用状态时:
this.state = {
newdiscoverPlanet: [
'sunp',
'twop',
'bluep',
'purplep',
'bluepurplep',
'redp',
'orangep'
],
};
_getRandomPlanet(){
var planetItem = this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)];
this.setState({
currentPlanet: planetItem,
});
}
如何从同一个状态得到不同的结果?
<Text>{this.state.currentPlanet}</Text>
<Text>{this.state.currentPlanet}</Text>
<Text>{this.state.currentPlanet}</Text>
我知道我可以使用 newdiscoverPlanet 的所有项目再添加两个不同的状态,但是 1) 我有机会获得相同的结果 2) 对于可能有更简单解决方案的东西来说,这似乎太长了。
【问题讨论】:
-
你能发布你的完整状态对象吗?您当前的状态没有
currentPlanet`property.
标签: javascript reactjs react-native random jsx