简体   繁体   English

反应原生。 我刚开始学习 React Native,我有一个关于 return 声明的问题

[英]React Native. I have just started learning react native and I have a question about return statement

I have a question about return statement.There is a return already wrapped whole code why are we returning again?Why there are two return in this code?我有一个关于return语句的问题。有一个return已经包裹了整个代码为什么我们又要返回?为什么这段代码中有两个return? Thanks for your help.谢谢你的帮助。

return (
    <FlatList
      showsVerticalScrollIndicator={false}
      keyExtractor={(friend) => friend.name}
      data={friends}
      renderItem={({ item }) => {
       return <Text style={styles.text}>
          {item.name} - Age: {item.Age}
        </Text>;
      }}
    />
  );```

If you look at the documentation for the renderItem prop on a FlatList , you'll see that it is expecting a function.如果您查看renderItem上的FlatList文档,您会发现它需要一个 function。 That return statement is part of that function.该返回语句是 function 的一部分。

After some research, you can't find something conclusive about the why.. But you can look into Documentation and see that it expects to return a React Element.经过一些研究,您无法找到有关原因的结论性信息。但是您可以查看文档并查看它期望返回一个 React 元素。

Also, if you look into the source itself you can see that the return statement exists because of the iteration:此外,如果您查看源代码本身,您可以看到由于迭代而存在 return 语句:

在此处输入图像描述

Since the Data attribute aways expect an array:由于Data属性远离期望一个数组:

在此处输入图像描述

Your renderItem should iterate it for each ocurrence and return the Reat Element您的renderItem应该为每次出现迭代它并返回 Reat 元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM