简体   繁体   English

在react native中如何使用json数据重复相同的渲染n次,请毫不犹豫地解释清楚

[英]In react native how to repeat same render n times using json data,please explain clearly without any hesitate

here I am trying to repeat one view it contains image,icon,and some text using fab button on press with the help of json and for loop,if it is done it is going to execute or it will throw the errors,suggest best method to do this.在这里,我试图重复一个视图,它包含图像、图标和一些文本,在 json 和 for 循环的帮助下使用 fab 按钮按下,如果它完成它将执行或它会抛出错误,建议最好的方法去做这个。

You could use the map function to repeat a component multiple times.您可以使用 map function 多次重复一个组件。

export default function AppExample({jsonList}) {

  const getCustomView = () => {
    return jsonList.map((item, index) => {
      return (
        <MyCostomView
          key={item.id}
          item={item}
        />
      );
    });
  };

  return (
      <View>
        {getCustomView()}
      </View>
  );
}

Here jsonList is an array of objects.这里的jsonList是一个对象数组。 MyCustomView is another component that includes your image text, use key with some unique id like image URL (if any). MyCustomView是另一个包含图像文本的组件,使用带有一些唯一 ID 的键,例如图像 URL(如果有)。

use Flatlist for rendering view multitple times使用 Flatlist 多次渲染视图

 <FlatList
        data={DATA}
        renderItem={renderItem}
        keyExtractor={item => item.id}
      />

where data can be your JSON data and renderItem Will be your View that you want to repeat.其中数据可以是您的 JSON 数据,而 renderItem 将是您要重复的视图。

For detail, example check this有关详细信息,请检查示例

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

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