Jacquob Cast
Jacquob Cast

Reputation: 156

React Native - Return multiple times in a loop

i'm trying to render some things in my select_types function. For that, i'm calling my function in a loop.

But my problem is : I can't return more than one time in a loop, but i need to render more than one thing.

for (let [keys, values] of Object.entries(test1[0])) {
       return (
        <View>
          {this.select_types(values.type, keys)}
         </View>
       )
    }

Do i need to replace all my loop ? Can I keep my loop but do multiple return in react native ?

Upvotes: 1

Views: 893

Answers (1)

AdriSolid
AdriSolid

Reputation: 2825

Try saving all Views in an array, then return them, something like:

const results = [];
for (let [keys, values] of Object.entries(test1[0])) {
  results.push(
    <View>
      {this.select_types(values.type, keys)}
    </View>
  );
}

return results;

Upvotes: 2

Related Questions