react-native数组映射未迭代

我正在尝试使用map方法遍历数组中的所有项目,但是当数组中有项目时,它不会遍历任何项目。我想念什么吗?

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

console.log输出:

[
   {
      "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"
   }
]
评论
  • lsint
    lsint 回复
    answers(answers: IAnswer[]) {
            console.log(answers);
            return (
                <View>
                    {
                        answers.map((answer) => ( //replace { to ( here
                            <Text key={answer.id}>Test</Text>
                        ) //replace } to ) here
                       )}
                </View>
            )
        }
    
  • 胖子
    胖子 回复

    You haven't returned the result of your map function

    您可以像这样显式地返回它

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

    或使用像

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