React-Native中Array渲染的优化

 

例如用Push加进去的数据:

constructor(props){
    super(props);
    this.state = {
      blnUpdate: false,
    };
    this.testArr = [];
    for(var i=0;i<5;i++){
      this.testArr.push(
        <Text key={i} style={{fontSize:20, color: 'red'}} onPress={this.changeChild.bind(this, i)}>
          这是{i}
        </Text>
      );
    }
    console.log(this.testArr);
  }

 

增加 key  和 ref

优化后的代码如下:

for(var i=0;i<5;i++){
      this.testArr.push(
        <Text key={i} ref={'text'+i} style={{fontSize:20, color: 'red'}} onPress={this.changeChild.bind(this, i)}>
          这是{i}
        </Text>
      );
    }

 

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/11179286.html

转载请著名出处!谢谢~~

 

相关文章:

  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2023-02-10
  • 2021-07-11
  • 2021-11-06
  • 2022-12-23
  • 2022-02-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2022-12-23
  • 2021-07-25
  • 2021-09-03
相关资源
相似解决方案