【问题标题】:React Native cloneWithRows error - object undefinedReact Native cloneWithRows错误 - 对象未定义
【发布时间】:2015-10-26 00:32:14
【问题描述】:

我对框架很陌生。 我正在使用 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
}
]}

然而,我在 cloneWithRows 上收到“未定义不是对象(评估 'dataSource.rowIdentities')”错误。 当我在 iOS 8.4 和 9.1 上运行模拟时出现此错误。 我可能错过了一些小东西,并且已经被困了几个小时。 有什么想法吗?

【问题讨论】:

    标签: ios node.js react-native


    【解决方案1】:

    看看代码,我猜你想在列表视图中显示状态。为此将您的代码更改为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 };

    【讨论】:

    • 也试过了。我回来了'TypeError:无法读取未定义的属性'cloneWithRows' ...
    • @EvgenyRoskach 我已经更新了我的答案,请检查是否能解决您的问题
    • 谢谢!有效。我修改了以下方式: this.state = { searchString: '', isLoading: false, message: '', dataSource: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2, }) , };
    • @EvgenyRoskach 很高兴它对你有用,请不要接受我的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多