Javascript 是否从react native中的列表中获取唯一项?
以下是我的清单:Javascript 是否从react native中的列表中获取唯一项?,javascript,arrays,reactjs,react-native,jsx,Javascript,Arrays,Reactjs,React Native,Jsx,以下是我的清单: newdiscoverPlanet: [ require('../img/sunp.png'), require('../img/twopp.png'), require('../img/bluep.png'), require('../img/purplep.png'), require('../img/bluepurplep.png'), require('../img/redp.png'), require('../img/orangep.pn
newdiscoverPlanet: [
require('../img/sunp.png'),
require('../img/twopp.png'),
require('../img/bluep.png'),
require('../img/purplep.png'),
require('../img/bluepurplep.png'),
require('../img/redp.png'),
require('../img/orangep.png')
],
我将所有这些都放在一个函数中,然后在这里使用Math
:
getRandomPlanet = () =>{
var planetItem = this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)];
this.setState({
changePlanet: planetItem,
});
}
然后我将它们放在选项卡中,从列表中获得唯一的图像:
_renderTabIndicator() {
let tabs = [{
text: `${this.state.tags.toLowerCase()}`,
iconSource: `${this.state.changePlanet}`
},{
text: `${this.state.tags2.toLowerCase()}`,
iconSource: `${this.state.changePlanet}`
},{
text: `${this.state.tags3.toLowerCase()}`,
iconSource: `${this.state.changePlanet}`
}];
return <PagerTabIndicator tabs={tabs} />;
}
\u renderTabIndicator(){
设制表符=[{
文本:`${this.state.tags.toLowerCase()}`,
iconSource:`${this.state.changePlanet}`
},{
文本:`${this.state.tags2.toLowerCase()}`,
iconSource:`${this.state.changePlanet}`
},{
文本:`${this.state.tags3.toLowerCase()}`,
iconSource:`${this.state.changePlanet}`
}];
返回;
}
但每次我加载页面时,我都会从每个源获得相同的图像。有没有办法让它们独一无二?如何在React Native中执行类似操作:
问题在于,您访问相同的值(this.state.changePlanet)3次,并期望得到不同的结果。 我想你只需要让方法返回一个随机行星,如下所示:
getRandomPlanet = () => {
return this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)];
}
然后您可以调用此按钮3次,您将获得3个不同的图像:
_renderTabIndicator() {
let tabs = [{
text: `${this.state.tags.toLowerCase()}`,
iconSource: this.getRandomPlanet()
},{
text: `${this.state.tags2.toLowerCase()}`,
iconSource: this.getRandomPlanet()
},{
text: `${this.state.tags3.toLowerCase()}`,
iconSource: this.getRandomPlanet()
}];
return <PagerTabIndicator tabs={tabs} />;
}
然后打电话:
_renderTabIndicator() {
const planets = this.getRandomPlanets(3);
let tabs = [{
text: `${this.state.tags.toLowerCase()}`,
iconSource: planets[0]
},{
text: `${this.state.tags2.toLowerCase()}`,
iconSource: planets[1]
},{
text: `${this.state.tags3.toLowerCase()}`,
iconSource: planets[2]
}];
return <PagerTabIndicator tabs={tabs} />;
}
\u renderTabIndicator(){
const planets=this.getRandomPlanets(3);
设制表符=[{
文本:`${this.state.tags.toLowerCase()}`,
iconSource:行星[0]
},{
文本:`${this.state.tags2.toLowerCase()}`,
iconSource:行星[1]
},{
文本:`${this.state.tags3.toLowerCase()}`,
iconSource:行星[2]
}];
返回;
}
问题在于,您访问相同的值(this.state.changePlanet)3次,并期望得到不同的结果。
我想你只需要让方法返回一个随机行星,如下所示:
getRandomPlanet = () => {
return this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)];
}
然后您可以调用此按钮3次,您将获得3个不同的图像:
_renderTabIndicator() {
let tabs = [{
text: `${this.state.tags.toLowerCase()}`,
iconSource: this.getRandomPlanet()
},{
text: `${this.state.tags2.toLowerCase()}`,
iconSource: this.getRandomPlanet()
},{
text: `${this.state.tags3.toLowerCase()}`,
iconSource: this.getRandomPlanet()
}];
return <PagerTabIndicator tabs={tabs} />;
}
然后打电话:
_renderTabIndicator() {
const planets = this.getRandomPlanets(3);
let tabs = [{
text: `${this.state.tags.toLowerCase()}`,
iconSource: planets[0]
},{
text: `${this.state.tags2.toLowerCase()}`,
iconSource: planets[1]
},{
text: `${this.state.tags3.toLowerCase()}`,
iconSource: planets[2]
}];
return <PagerTabIndicator tabs={tabs} />;
}
\u renderTabIndicator(){
const planets=this.getRandomPlanets(3);
设制表符=[{
文本:`${this.state.tags.toLowerCase()}`,
iconSource:行星[0]
},{
文本:`${this.state.tags2.toLowerCase()}`,
iconSource:行星[1]
},{
文本:`${this.state.tags3.toLowerCase()}`,
iconSource:行星[2]
}];
返回;
}
这有点让人困惑。。您是否希望this.state.changePlanet
每个选项卡都有不同的值?@azium yes i amwell这就像说var a=1
然后希望a
不等于1
。您没有设置3个不同的值,而是设置一个值并使用该值3次。您不需要任何状态,只需调用this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)]
其中iconSource
是<代码>iconSource:请注意,这将获得不同的值,而不一定是唯一的。但是您应该从这里开始,但是在不同的iconSource中,图像是否有可能彼此相同?这有点令人困惑。。您是否希望this.state.changePlanet
每个选项卡都有不同的值?@azium yes i amwell这就像说var a=1
然后希望a
不等于1
。您没有设置3个不同的值,而是设置一个值并使用该值3次。您不需要任何状态,只需调用this.state.newdiscoverPlanet[Math.floor(Math.random()*this.state.newdiscoverPlanet.length)]
其中iconSource
是<代码>iconSource:请注意,这将获得不同的值,而不一定是唯一的。但是您应该从这里开始,但是在不同的iconSource中,图像是否有可能彼此相同?