How to pass sentences to a variable and then display randomly

Asked

Viewed 279 times

1

Well, I started programming yesterday in React Native and still do not understand very well how to do things.... I need to pass a series of sentences to several variables, each variable a sentence, and then present them randomly.

onPlay(){
      // const um = "testeeeeee";
}

// EVENTS
// SPECIFIC METHODS

render() {   
    return (
      <View style={styles.container}>

        <View style={{flex:1, backgroundColor: 'skyblue'}}>
          <Text style={styles.title}>
            Would You Rather
          </Text>
          <TouchableHighlight style={[styles.primary_button,styles.medium_button]} onPress={this.onPlay.bind(this)}>
          <Text style={styles.medium_button_text}>
            test
          </Text>
          </TouchableHighlight>
        </View>

        <View style={{flex:1, backgroundColor: 'steelblue'}}>
          <TouchableHighlight style={[styles.primary_button,styles.medium_button]} onPress={this.onPlay.bind(this)}>
          <Text style={styles.medium_button_text}>
            test2
          </Text>
          </TouchableHighlight>
        </View>
      </View>
    );
}
  • You don’t have state nor props? where these phrases come from?

  • the phrases I was thinking of passing them by hand to the variables directly in the code

1 answer

3


You must have those phrases in props or in the state component. If this is the case, to avoid duplicating code you can do so:

constructor(props) {
    super(props);
    this.state = {
        frases: [
            {title: 'frase 1', conteudo: 'Olá!'},
            {title: 'frase 2', conteudo: 'Olé!'}
    };
}

// para as misturar aleatoriamente
function misturador() {
    const array = this.state.frases.slice();
    let length = array.length;
    while (counter > 0) {
        let index = Math.floor(Math.random() * counter);
        length--;
        let temp = array[length];
        array[length] = array[index];
        array[index] = temp;
    }
    return array;
}
render() {   
    const frases = this.misturador().map(frase => (
        <View style={{flex:1, backgroundColor: 'skyblue'}}>
          <Text style={styles.title}>{frase.title}</Text>
          <TouchableHighlight style={[styles.primary_button,styles.medium_button]} onPress={this.onPlay.bind(this)}>
          <Text style={styles.medium_button_text}>{frase.conteudo}</Text>
          </TouchableHighlight>
        </View>
    ));
    return (
      <View style={styles.container}>
          {frases}
      </View>
    );
}

Another way, perhaps even better is to have a component for that text and manage within the component as the title is treated. Then you use it the same way, with a .map() from the phrase array to the component.

  • but so will appear all the phrases that are inside phrases: [ {title: 'phrase 1', content: 'Hello! '}, {title: 'phrase 2', content: 'Olé! '} }; right?

  • @Exact Alexandreseabra. If not all have title you have to put together a if/else somewhere, or do as I suggested at the end: a component that generates that apart so that logic is not inside the render of this component.

  • 1

    Okay, I’ll try, thanks

  • @Alexandreseabra I added another method, I had not noticed that you wanted the phrases randomly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.