lucster
lucster

Reputation: 179

react-native array map not iterating

I am trying to iterate over all items in my array, using the map method, but it doesn't iterate over any items while there are item in the array. Am I missing something?

answers(answers: IAnswer[]) {
        console.log(answers);
        return (
            <View>
                {
                    answers.map((answer) => {
                        <Text key={answer.id}>Test</Text>
                    })}
            </View>
        )
    }

console.log output:

[
   {
      "correct":false,
      "id":"11",
      "value":"De vorm van een zon"
   },
   {
      "correct":false,
      "id":"22",
      "value":"Een vierkant"
   },
   {
      "correct":true,
      "id":"33",
      "value":"Vierkant of rechthoekig"
   }
]

Upvotes: 1

Views: 189

Answers (2)

Waleed Nasir
Waleed Nasir

Reputation: 607

answers(answers: IAnswer[]) {
        console.log(answers);
        return (
            <View>
                {
                    answers.map((answer) => ( //replace { to ( here
                        <Text key={answer.id}>Test</Text>
                    ) //replace } to ) here
                   )}
            </View>
        )
    }

Upvotes: 1

Shubham Khatri
Shubham Khatri

Reputation: 281656

You haven't returned the result of your map function

You can either return it explicitly like

{
    answers.map((answer) => {
        return <Text key={answer.id}>Test</Text> // return the result
    })
}

or use implicit return like

{
    answers.map((answer) => ( // implicit return uses `()` brackets
        <Text key={answer.id}>Test</Text> 
    ))
}

Upvotes: 1

Related Questions