I have an array with some phrases that looks like this:
words = [
{ text: '...', key: 1},
{ text: '...', key: 2},
{ text: '...', key: 3},
{ text: '...', key: 4} ... ]
I want to display a random text from this array when I click a button. This is the code that I have so far:
import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
export default function App() {
var textValue = 'Change me'
words = [...]
const len = words.length;
const changeTextValue = () => {
textValue = words[Math.floor(Math.random() * len)].text
}
return (
<View style={styles.container}>
<Text>{textValue}</Text>
<Button onPress={changeTextValue} title='Press me'/>
<StatusBar style="auto" />
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'lightblue',
alignItems: 'center',
justifyContent: 'center',
},
});
But it doesn't work. Could you please help me with this? Thanks in advance.