I am new to react-native and i have problems with updating array elements ! I want to update values and then show it on the display. Here is my code so far :
import React from "react";
import {
StyleSheet,
View,
Button,
Text,
} from "react-native";
export default function App() {
let [currentElement, setCurrentElement] = React.useState(0);
const array = [0, 0, 0];
function nextEle() {
setCurrentElement(currentElement + 1);
}
function prevEle() {
setCurrentElement(currentElement - 1);
}
function setNum(num) {
array[currentElement] = [num];
}
return (
<View style={styles.container}>
<Button title="set array[0] = 1" onPress={setNum(1)} />
<Button title="set array[1] = 2" onPress={setNum(2)} />
<Button title="set array[2] = 3" onPress={setNum(3)} />
<Button title="Next element" onPress={nextEle} />
<Button title="Prev element" onPress={prevEle} />
<Text>{array[currentElement]}</Text>
<Text>current element is {currentElement}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
top: 10,
alignSelf: "center",
justifyContent: "center",
flexDirection: "column",
width: "80%",
position: "absolute",
},
});
The problem here is when i switch between values using button next and prev nothing happened. Only currentElement value is changed. Why array doesn't change values?