2015-10-26 78 views
0

我对框架相当陌生。 我正在使用React-Native中的dataSource制作基本ListView,并提取数据。React Native cloneWithRows错误 - 对象未定义

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 
} 
]} 

然而,我得到一个“未定义不是(评价‘dataSource.rowIdentities’)的物体”上cloneWithRows错误。 当我在iOS 8.4和9.1上运行模拟时出现此错误。 我可能错过了一些小小的东西,并且被困了好几个小时。 任何想法?

回答

3

看着代码,我想你想显示列表视图内的状态。对于更改您的代码this.state.dataSource.cloneWithRows(responseData.statuses)

ListViewDataSource源代码有详细的注释,说明此项功能期待的格式https://github.com/facebook/react-native/blob/62e8ddc20561a39c3c839ab9f83c95493df117c0/Libraries/CustomComponents/ListView/ListViewDataSource.js#L95-L110

更新:

设置状态对象的DataSource属性,在修改代码构造函数为

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

+0

试过了。我回来'TypeError:无法读取属性'cloneWithRows'的未定义'... –

+0

@EvgenyRoskach我已经更新了我的答案,请检查那将解决您的问题 – Rajeesh

+1

谢谢!有效。 我修改方式如下: this.state = { \t \t \t searchString的: '', \t \t \t isLoading:假, \t \t \t消息: '', \t \t \t的dataSource:新的ListView。数据源({ \t \t \t \t rowHasChanged:(R1,R2)=> R1 == R2, \t \t!}), \t \t \t \t \t}; –

相关问题