1
Good evening. I am learning the Hooks and would like help to solve the problem of updating the useState value from an array. I have the code to follow:
import React, { useState } from 'react';
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';
const Jantar = () => {
const opcoes = ['Hamburger', 'Chachorro-quente', 'Lasanha', 'Strogonoff'];
const [comidas, setComidas] = useState(opcoes[0]);
const onPress = () => {
setComidas(comidas => comidas + 1);
}
return (
<>
<Text style={styles.text}>Hoje você vai jantar: {comidas} </Text>
<TouchableOpacity style={styles.botao}>
<Text style={styles.text} onPress={onPress} >Escolher comida</Text>
</TouchableOpacity>
</>
);
}
export default function App() {
return (
<View style={styles.container}>
<Jantar />
</View>
);
}
I have an array of foods and a text field that says which food the person goes to dinner. Each click on the button should update the text field with the next item of the food array. I am unable to update the text field with the onPress function. By my analysis, I need to link, somehow, the variable "food" - which initializes with the first item of the food array - with the array "options". I don’t know how to do.
Thank you Virgil. You’ve helped so much.
– César Augusto
@Césaraugusto if your question is answered tick
– novic