简体   繁体   English

如何使用ListView.DataSource?

[英]How is ListView.DataSource used?

Just wondering how to read this script from the RN ListView Documentation : 只是想知道如何从RN ListView文档中读取此脚本:

var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});

What am I exactly passing into ListView.DataSource()? 我到底要传递给ListView.DataSource()的什么? I suppose it is an object with rowHasChanged declared as a function? 我想这是一个将rowHasChanged声明为函数的对象? Therefore, when the function is eventually called by whatever is holding it, it's parent knows to look for rowHasChanged, and use it just as how it is declared? 因此,当函数最终被持有它的人调用时,它的父级知道要查找rowHasChanged,并像声明它一样使用它?

So essentially, it's "just a requirement" that we have to define an object containing rowHasChanged when using ListView.DataSource? 因此,从本质上讲,使用ListView.DataSource时必须定义一个包含rowHasChanged的对象是“一项要求”?

Or am I interpreting this incorrectly. 还是我误解了这一点。

Thanks! 谢谢!

rowHasChanged is just a property to a callback to differ between two items in your data array. rowHasChanged只是回调的属性,该回调在数据数组中的两个项目之间有所不同。 At the end you only have to pass an array with values (or objects) to the ListView encapsulated within "cloneWithRows"-Method of the DataSource-Object. 最后,您只需要将带有值(或对象)的数组传递给封装在DataSource-Object的“ cloneWithRows” -Method中的ListView。

import React, { Component } from 'react';
import { AppRegistry, ListView, Text, View } from 'react-native';

class ListViewBasics extends Component {
  // Initialize the hardcoded data
  constructor(props) {
    super(props);
    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.state = {
      items: ds.cloneWithRows([
        'John', 'Joel', 'James', 'Jimmy', 'Jackson', 'Jillian', 'Julie', 'Devin'
      ])
    };
  }
  render() {
    return (
      <View style={{flex: 1, paddingTop: 22}}>
        <ListView
          dataSource={this.state.items}
          renderRow={(rowData) => <Text>{rowData}</Text>}
        />
      </View>
    );
  }
}

// App registration and rendering
AppRegistry.registerComponent('ListViewBasics', () => ListViewBasics);

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

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