1

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"
   }
]
1
  • Dont use answers.map((answer) ={...} ) like this. when you render tags , you can use round brackets like this answers.map((answer) => ( <Text key={answer.id}>Test</Text> )) Commented May 11, 2020 at 12:45

2 Answers 2

1

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> 
    ))
}
Sign up to request clarification or add additional context in comments.

Comments

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

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.