You should have these phrases in props
or state
of the component. That being the case, to avoid duplicating code you can do this:
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 handled. Then you use the same way, with a .map()
of the sentence array for the component.