Today i want to create array in react-native and update it via hook to display elements on the screen in sequence. So the final result should be 0 1 2 3 4 My code so far
import React, { useState, useRef } from "react";
import { StyleSheet, Text, View, Button } from "react-native";
export default function App() {
const [arr, setArr] = useState([]);
function test() {
for (var i = 0; i < 5; i++) {
setArr([...arr, i]);
}
}
return (
<View style={styles.container}>
<Text>{arr}</Text>
<Button title="change next index" onPress={() => test()} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
But this shows me only the last elements of the array which is 4. How can i display all elements in sequence on the screen?