Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop in react-native

Tags:

react-native

I want to make a list of fields depending on the number of the player that user has selected. I wanted to make something like this:

generatePaymentField() {
    var noGuest = this.state.guest;
    var payment = 
    <View>
        <View>
            <View><Text>No</Text></View>
            <View><Text>Name</Text></View>
            <View><Text>Preference</Text></View>
        </View>;
        
    for (var i=0; i < noGuest; i++) {
        payment = payment + 
            <View>
                <View>
                    <TextInput />
                </View>
                <View>
                    <TextInput />
                </View>
                <View>
                    <TextInput />
                </View>
            </View>;
    }
    return payment;
}

render () {
    var payment = this.generatePaymentField();
    this.setState({payment : payment});
    return (
        <View>
            {this.state.payment}
        </View>;
    )
}

But react-native regarded the syntax above as 'unexpected token' pointing at the for loop line. Is there any other way I can achieve doing this?

like image 514
Kelvin Aliyanto Avatar asked Jan 07 '16 06:01

Kelvin Aliyanto


People also ask

How do you loop in react native?

To loop and render elements in React Native, we can use the JavaScript array map method. to call arr. map with a callback to return the Text component with the entry a as the text. We set the key prop to a unique value for each entry so that React can identify the rendered components.

Can I use a for loop in React?

It is very popular to use loops like for-loop (in most cases the fastest one), for-in, or for-of to iterate through elements. That method is useful when we use separate functions to render part of components, and it's the best method for performance.

What is forEach loop in React?

In this article, you are going to learn about how to use forEach loop in react. In JavaScript, the forEach() loop is considered as an array method that is used to iterate each item of an array with the help of a callback function.

Can I use React JSX loops in React Native?

Most of these ways are covered in this article about React JSX loops, and although it's using React examples, everything from it can be used in React Native. Please check it out if you're interested in this topic!

How to for loop in react?

How to For Loop in React (With Examples) To For Loop or Map in React. As with most things in web development, there are multiple ways to loop, or iterate,... Using The Map Function in React. Introduced in ES6, the Map array function is by far my most used method of iterating... Use a Unique Key for ...

How do I iterate through an array in react JSX?

Out of the three iterators above, our best option to iterate over an array in React inside of JSX is the Map function. Let’s begin by exploring how we can use the Map iterator to loop through elements in an array and render out some HTML for each of those elements.

How to loop through an object in react using Axios?

After setting up the initial React project and calling data from the API using axios our object data will look something like this. Now lets loop through this object using different methods available. The for/in loops through the properties of an object. The block of code inside the loop will be executed once for each property.


2 Answers

This should work

render(){

	var payments = [];

	for(let i = 0; i < noGuest; i++){

		payments.push(
			<View key = {i}>
				<View>
					<TextInput />
				</View>
				<View>
					<TextInput />
				</View>
				<View>
					<TextInput />
				</View>
			</View>
		)
	}
	
	return (
		<View>
			<View>
				<View><Text>No</Text></View>
				<View><Text>Name</Text></View>
				<View><Text>Preference</Text></View>
			</View>

			{ payments }
		</View>
	)
}
like image 168
vinay Avatar answered Oct 19 '22 10:10

vinay


render() {
var myloop = [];

for (let i = 0; i < 10; i++) {
  myloop.push(
    <View key={i}>
    <Text>{i}</Text>
    </View>
  );
}

 return (
      
        <View >
          <Text >Welcome to React Native!</Text>
           {myloop}
        </View>
       
      
    );
  }
}
like image 8
Karan KJ Avatar answered Oct 19 '22 10:10

Karan KJ