简体   繁体   English

在React Native中如何通过相同的状态调用获得不同的结果?

[英]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.

相关问题 如何从 themoviedb api 获取多页结果并存储在 React Native 中的 state 中? - How do I get multiple pages of result from themoviedb api and store in state in React Native? React Native - 在子组件中调用相同的 Function 但结果不同 - React Native - Same Function call in child component but different result 如何在本机反应中从不同组件更改另一个组件的 state? - How do I change state of another component from different component in react native? 如何在本机反应中单击按钮时使用不同的参数调用相同的 api - How to call same api with different params on click of button in react native 我如何从其他组件中获取this.state.value - How do I get a this.state.value from other components in react native 反应:mapStateToProps - 我如何获得 state? - React: mapStateToProps - How do I get state? 如何使用javascript获得相同的结果? - How do I get the same result with javascript? React Native - 如何使用相同的代码在 react-native 的两个不同页面中获得相同的输出? - React Native - How to get same output in two different pages in react-native with same code? 我怎么能叫反应状态不同的js - how can i call react state different js 我如何导出XMLHttpRequest结果以响应本机? - How do I export XMLHttpRequest result in react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM