React Native cloneWithRows错误 - 对象未定义

时间:2015-10-26 00:32:15

标签: ios node.js react-native

我对这个框架很陌生。 我正在使用React-Native中的dataSource创建一个基本的ListView,其中包含我获取的数据。

constructor(props){
    super(props);
    var dataSource = new ListView.DataSource({
        rowHasChanged: (r1, r2) => r1 !== r2,
    });
    this.state = {
        searchString: '',
        isLoading: false,
        message: '',
        jsonData: '',
    };
}


_fetchData(query){

    console.log(query);
    this.setState({ isLoading: true });

    fetch(query)
        .then(response => response.json())
        .then(responseData => {
            this._handleResponse(responseData);
            this.setState({
                message: 'seems like it worked!',
                dataSource: this.state.dataSource.cloneWithRows( responseData ),
            });
        })
        .catch(error => 
            this.setState({
                isLoading: false,
                message: this.state.message + ' --- ' + 'Something bad happened' + error
            })
        ).done(); 

}

我获取的数据是来自Twitter API响应的摘录:

{  "statuses": [
{
  "coordinates": null,
  "favorited": false,
  "truncated": false,
  "created_at": "Mon Sep 24 03:35:21 +0000 2012",
  "id_str": "250075927172759552",
  "retweet_count": 0,
  "in_reply_to_status_id_str": null,
  "id": 250075927172759552
}
]}

然而,我得到一个" undefined不是一个对象(评估' dataSource.rowIdentities')" cloneWithRows上的错误。 在iOS 8.4和9.1上运行模拟时会出现此错误。 我可能会遗漏一些小东西并且已经被困了好几个小时。 有什么想法吗?

1 个答案:

答案 0 :(得分:3)

查看代码,我想你想在listview中显示状态。为此,将代码更改为this.state.dataSource.cloneWithRows(responseData.statuses)

ListViewDataSource源代码有详细注释,解释了此函数期望的格式https://github.com/facebook/react-native/blob/62e8ddc20561a39c3c839ab9f83c95493df117c0/Libraries/CustomComponents/ListView/ListViewDataSource.js#L95-L110

<强>更新

在state对象上设置dataSource属性,将构造函数中的代码更改为

this.state = { searchString: '', isLoading: false, message: '', jsonData: '', dataSource: dataSource };