0

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?

2
  • you need to display one by one or show it all Commented Jun 23, 2021 at 13:40
  • @prasanth show it all Commented Jun 23, 2021 at 13:45

3 Answers 3

1

React state update is async. On surface it looks like you are updating state 5 times. but the state is being updated only once and when it get's update i value is already 4.

function test() {
  for (let i = 0; i < 5; i++) {
    setArr(prevState => [...prevState, i]);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

ye... I got 05555 right now istead of 01234
in loop use let instead of var
0

You need iterate array with Array#map

Codesandbox Demo

 <View style={styles.container}>
      {arr && arr.map(a=>(<Text key={a}>{a}</Text>))}
      <Button title="change next index" onPress={() => test()} />
 </View>

And also better setArr call on after map instead of inside the loop. Check my demo link . Because each time call unnecessary render on the loop

function test() {
    let newArr = [...arr];
    for (var i = 0; i < 5; i++) {
      newArr.push(i);
    }
    setArr(newArr);
  }

Comments

0

You need to join all elements of the array

return (
  <View style={styles.container}>
    <Text>{arr.join(' ')}</Text>
    <Button title="change next index" onPress={() => test()} />
  </View>
);

or if you want to wrap each number in an own element

return (
  <View style={styles.container}>
    {
      arr.map(num => (<Text key={num}>{num}</Text>))
    }
    <Button title="change next index" onPress={() => test()} />
  </View>
);

To not have performance issues, react also waits for the end of a function call before re-rendering (and update states), so in your for loop, you always pass an empty array with the current index into the state

setArr(prev=> [...prev, i]);

will give you the current value here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.