[英]How do I get a different result with the same state call in React Native?
My apologies for the confusing wording of the question. 对于这个令人困惑的措词,我深表歉意。 Basically when I call state from here: 基本上,当我从这里调用状态时:
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,
});
}
How do I get a different result from the same state? 从同一个状态如何获得不同的结果?
<Text>{this.state.currentPlanet}</Text>
<Text>{this.state.currentPlanet}</Text>
<Text>{this.state.currentPlanet}</Text>
I know I could just add two more different states with all the items of newdiscoverPlanet
but 1) I have a chance of getting the same results 2) It seems too lengthy for something that might have an easier solution. 我知道我可以为newdiscoverPlanet
所有项目添加两个不同的状态,但是1)我有机会获得相同的结果2)对于某些可能具有更简单解决方案的东西来说似乎太长了。
Don't put the randomly generated name in the state, but instead, call the function to generate a random name multiple times in your render
function. 不要在状态中放入随机生成的名称,而是在render
函数中多次调用该函数以生成随机名称。
Basically something like that should do the trick: 基本上像这样的事情应该可以解决问题:
_getRandomPlanet(){
var planetItem = this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)];
return planetItem
}
And in your JSX: 在您的JSX中:
<Text>{this._getRandomPlanet()}</Text>
<Text>{this._getRandomPlanet()}</Text>
<Text>{this._getRandomPlanet()}</Text>
First of all if newdiscoverPlanet
is constant it shouldn't be in the state (a file constant, or static member, an instance or even a prop member would do but it's not really a state of the component). 首先,如果newdiscoverPlanet
是常量,则不应处于状态(文件常量,静态成员,实例甚至是prop成员都可以,但实际上不是组件的状态)。
Then from what I understand of your questions it seems that you want a random selection of newDiscoverPlanet
instead of just one. 从我对您的问题的了解来看,您似乎希望随机选择newDiscoverPlanet
而不是一个。
And from what I read from a comment it also seems that you need to import image files for each planet. 从我的评论中可以看出,似乎还需要为每个行星导入图像文件。
So what about: 那么呢:
import sunpImg from '../img/sunp.png';
import twopImg from '../img/twop.png';
import bluepImg from '../img/bluep.png';
import purplepImg from '../img/purplep.png';
import bluepurplepImg from '../img/bluepurplep.png';
import redpImg from '../img/redp.png';
import orangepImg from '../img/orangep.png';
const planetsObj = [
{ name:'sunp', img: sunpImg },
{ name:'twop', img: twopImg },
{ name:'bluep', img: bluepImg },
{ name:'purplep', img: purplepImg },
{ name:'bluepurplep', img: bluepurplepImg },
{ name:'redp', img: redpImg },
{ name:'orangep', img: orangepImg },
];
class YouComponent extends Component {
state = {
randomPlanets: this.getRandomPlanets()
}
getRandomPlanets() {
// Note: since the randomization relies on random sorting
// you won't have the same planet twice, if you want a
// subset (less planets) just use .slice(numOfItem)
return [...planetsObj].sort(() => parseInt(Math.random() * 3, 10) - 1);
}
updateRandomPlanets() {
this.setState(() => ({ randomPlanets: this.getRandomPlanets() }));
}
render() {
const { randomPlanets } = this.state;
// Note: if you randomize during render the renders
// won't be consistent and the display won't be controllable
return (
{randomPlanets.map(planet => (
<div>
<img src={planet.img} />
<Text>{planet.name}</Text>
</div>
))}
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.