Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6212

Rendering random array element from state (React Native)

$
0
0

I'm trying to render a random array item from state so that (e.g - [0] might change to [2] on reload.]

Here is what I tried so far, any tips or ideas please?

here is my state:

     state = {      randomItem: ['one', 'two', 'three', 'four'],    },    selected: null,    clicked: false,  };

here is my handleClick function which will randomise the items grabbed from array

  handleClick = () => {    this.setState({      clicked: true,      selected: this.state.randomItem.selected[        Math.floor(Math.random() * this.state.selected.length)      ],    });  };

here is how im trying to return it

<View><TouchableOpacity onPress={this.handleClick}><Text>{this.state.clicked && this.state.selected}</Text></TouchableOpacity></View>

Viewing all articles
Browse latest Browse all 6212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>