Javascript 如何生成<;Text/>;使用react native的循环中的组件?

Javascript 如何生成<;Text/>;使用react native的循环中的组件?,javascript,react-native,Javascript,React Native,我想知道如何从一个存储字符串数组的变量生成组件,该变量带有一个循环(或类似的东西)。例如,我有一个数组: const arr = ['Hello', 'Awesome', 'Stack','Over','Flow', 'Have', 'A', 'Nice', 'Day']; 我写了这个方法: function renderT() { return arr.map(obj => { <Text>{obj}</Text>; }); } 因此,我在屏幕

我想知道如何从一个存储字符串数组的变量生成组件,该变量带有一个循环(或类似的东西)。例如,我有一个数组:

const arr = ['Hello', 'Awesome', 'Stack','Over','Flow', 'Have', 'A', 'Nice', 'Day'];
我写了这个方法:

function renderT() {
  return arr.map(obj => {
    <Text>{obj}</Text>;
  });
}
因此,我在屏幕上没有任何错误或文本。我尝试使用不同类型的循环
for
forEach
map
,但没有成功。您能告诉我或建议我如何实施它的正确方法吗?

您应该更换

function renderT() {
  return arr.map(obj => {
    <Text>{obj}</Text>;
  });
}
函数renderT(){
返回arr.map(obj=>{
{obj};
});
}
据此:

function renderT(arr) {
  return arr.map(obj => {
    return <Text>{obj}</Text>;
  });
}
return (
  <View style={styles.row}>{renderT(arr)}</View>
)
函数渲染(arr){
返回arr.map(obj=>{
返回{obj};
});
}
这是:

return (
  <View style={styles.row}>{renderT()}</View>
)
返回(
{renderT()}
)
据此:

function renderT(arr) {
  return arr.map(obj => {
    return <Text>{obj}</Text>;
  });
}
return (
  <View style={styles.row}>{renderT(arr)}</View>
)
返回(
{renderT(arr)}
)
这两个错误是函数renderT中缺少返回和参数。然后必须使用正确的参数(数组)调用函数。

应该替换

function renderT() {
  return arr.map(obj => {
    <Text>{obj}</Text>;
  });
}
函数renderT(){
返回arr.map(obj=>{
{obj};
});
}
据此:

function renderT(arr) {
  return arr.map(obj => {
    return <Text>{obj}</Text>;
  });
}
return (
  <View style={styles.row}>{renderT(arr)}</View>
)
函数渲染(arr){
返回arr.map(obj=>{
返回{obj};
});
}
这是:

return (
  <View style={styles.row}>{renderT()}</View>
)
返回(
{renderT()}
)
据此:

function renderT(arr) {
  return arr.map(obj => {
    return <Text>{obj}</Text>;
  });
}
return (
  <View style={styles.row}>{renderT(arr)}</View>
)
返回(
{renderT(arr)}
)

这两个错误是函数renderT中缺少返回和参数。然后您必须使用正确的参数(数组)调用函数。

您的
arr
上的
map
函数没有返回任何内容,这就是您没有看到任何文本的原因

尝试将函数
renderT()
替换为:

function renderT() {
  return arr.map(obj => (<Text>{obj}</Text>));
}
函数renderT(){
返回arr.map(obj=>({obj}));
}

(只需将
{}
值周围的
替换为
()

您的
arr
上的
map
函数没有返回任何内容,这就是您没有查看任何文本的原因

尝试将函数
renderT()
替换为:

function renderT() {
  return arr.map(obj => (<Text>{obj}</Text>));
}
函数renderT(){
返回arr.map(obj=>({obj}));
}

(只需将
{}
值周围的
替换为
()

需要注意的是,如果遗漏,将导致错误和/或意外行为。考虑下面对代码的调整,这些代码应该解决运行时错误:

function renderT() {

  // Ensure arr is defined in scope of, or is "visible" to, renderT()
  const arr = ['Hello', 'Awesome', 'Stack','Over','Flow', 'Have', 'A', 'Nice', 'Day'];

  return arr.map((obj, index) => {
    // Compose a unique key for the text element. A unique key is need when 
    // rendering lists of components
    const key = index;

    // Add return to ensure the text element is returned from map callback
    return <Text key={key}>{obj}</Text>;
  });
}
函数renderT(){
//确保arr在renderT()的范围内定义,或对renderT()可见
const arr=['Hello','Awesome','Stack','Over','Flow','Have','A','Nice','Day'];
返回arr.map((对象,索引)=>{
//为文本元素组成一个唯一键。当
//呈现组件列表
常量键=索引;
//添加return以确保从映射回调返回文本元素
返回{obj};
});
}
需要注意的是
属性-应该为列表中呈现的组件提供该属性(即,在您的情况下为

需要
道具的原因是。呈现的每个项目的每个
键必须是唯一的-通常应避免使用映射回调的
索引,但是不能保证在
arr
中看到值的唯一性


希望有帮助

有一些微妙之处需要注意,如果遗漏,将导致错误和/或意外行为。考虑下面对代码的调整,这些代码应该解决运行时错误:

function renderT() {

  // Ensure arr is defined in scope of, or is "visible" to, renderT()
  const arr = ['Hello', 'Awesome', 'Stack','Over','Flow', 'Have', 'A', 'Nice', 'Day'];

  return arr.map((obj, index) => {
    // Compose a unique key for the text element. A unique key is need when 
    // rendering lists of components
    const key = index;

    // Add return to ensure the text element is returned from map callback
    return <Text key={key}>{obj}</Text>;
  });
}
函数renderT(){
//确保arr在renderT()的范围内定义,或对renderT()可见
const arr=['Hello','Awesome','Stack','Over','Flow','Have','A','Nice','Day'];
返回arr.map((对象,索引)=>{
//为文本元素组成一个唯一键。当
//呈现组件列表
常量键=索引;
//添加return以确保从映射回调返回文本元素
返回{obj};
});
}
需要注意的是
属性-应该为列表中呈现的组件提供该属性(即,在您的情况下为

需要
道具的原因是。呈现的每个项目的每个
键必须是唯一的-通常应避免使用映射回调的
索引,但是不能保证在
arr
中看到值的唯一性


希望有帮助

map函数应该有一个返回值。您是否尝试返回arr.map(obj=>return{{{obj};});你就快到了:1。在renderT中,哪里定义了
arr
?2.替换
map
函数中的大括号,使其实际返回3。为每个
文本添加
道具
映射函数应具有返回值。您是否尝试返回arr.map(obj=>return{{{obj};});你就快到了:1。在renderT中,哪里定义了
arr
?2.替换
map
函数中的大括号,使其实际返回3。在每个
文本中添加
道具
@MatzHeri您非常受欢迎-项目一切顺利:)@MatzHeri您非常受欢迎-项目一切顺利:)