React-Native更新列表视图数据源

时间:2015-07-31 05:26:33

标签: javascript ios arrays datasource react-native

我有一个iOS应用程序,我正在使用react-native。 Game类包含一个ListView组件。我在构造函数中设置状态并包含dataSource。我现在有一个硬编码的数据数组,我存储在不同的状态属性(this.state.ds)中。然后在componentDidMount我使用cloneWithRows方法克隆我的this.state.ds作为视图的dataSource。就ListViews来说,这是非常标准的并且运行良好。这是代码:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';

 var React = require("react-native");
 var { StyleSheet, Text, View, ListView, TouchableHighlight } = React;

class Games extends React.Component {
constructor(props) {
    super(props);
    var ds = new ListView.DataSource({
        rowHasChanged: (r1, r2) => r1 != r2
    });
    this.state = {
        ds: [
            { AwayTeam: "TeamA", HomeTeam: "TeamB", Selection: "AwayTeam" },
            { AwayTeam: "TeamC", HomeTeam: "TeamD", Selection: "HomeTeam" }
        ],
        dataSource: ds
    };
}

componentDidMount() {
    this.setState({
        dataSource: this.state.dataSource.cloneWithRows(this.state.ds)
    });
}
pressRow(rowData) {
    var newDs = [];
    newDs = this.state.ds;
    newDs[0].Selection = newDs[0] == "AwayTeam" ? "HomeTeam" : "AwayTeam";
    this.setState({
        dataSource: this.state.dataSource.cloneWithRows(newDs)
    });
}

renderRow(rowData) {
    return (
        <TouchableHighlight
            onPress={() => this.pressRow(rowData)}
            underlayColor="#ddd"
        >
            <View style={styles.row}>
                <Text style={{ fontSize: 18 }}>
                    {rowData.AwayTeam} @ {rowData.HomeTeam}{" "}
                </Text>
                <View style={{ flex: 1 }}>
                    <Text style={styles.selectionText}>
                        {rowData[rowData.Selection]}
                    </Text>
                </View>
            </View>
        </TouchableHighlight>
    );
}
render() {
    return (
        <ListView
            dataSource={this.state.dataSource}
            renderRow={this.renderRow.bind(this)}
        />
    );
}
}
var styles = StyleSheet.create({
 row: {
    flex: 1,
    flexDirection: "row",
    padding: 18,
    borderBottomWidth: 1,
    borderColor: "#d7d7d7"
},
selectionText: {
    fontSize: 15,
    paddingTop: 3,
    color: "#b5b5b5",
    textAlign: "right"
}
});


module.exports = Games

我遇到的问题来自pressRow方法。当用户按下该行时,我希望更改选择并使其在设备上呈现更改。通过一些调试,我注意到即使我正在更改Selection数组中对象的newDs属性,同一属性也会更改this.state.ds中的对象,同样会更改对象在this.state.dataSource._dataBlob.s1。通过进一步调试,我发现由于其他数组已经发生变化,ListView的DataSource对象无法识别变化,因为当我设置状态并调用rowHasChanged时,数组就是克隆与数组this.state.dataSource._dataBlob.s1匹配,因此它看起来不像是一个变化,也不会重新渲染。

有什么想法吗?

3 个答案:

答案 0 :(得分:18)

试试这个:

pressRow(rowData){

    var newDs = [];
    newDs = this.state.ds.slice();
    newDs[0].Selection = newDs[0] == "AwayTeam" ? "HomeTeam" : "AwayTeam";
    this.setState({
      dataSource: this.state.dataSource.cloneWithRows(newDs)
    })

}

这应该是数组的副本,然后可以独立于this.state.ds中的原始数组进行修改。

答案 1 :(得分:18)

如果有人像我一样遇到这个问题,这就是完整的解决方案。

基本上,ListView组件似乎存在错误,您需要重建ListView数据源中更改的每个项目以重绘它。

首先,创建数据源和数组的副本并将其保存到状态。在我的例子中,foods是数组。

constructor(props){
    super(props);
    var ds = new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
    });
    this.state = {
        dataSource: ds.cloneWithRows(foods),
        db: foods,
    };
}

如果要更改数据源中的内容,请将保存到的数组的副本复制到状态,使用更改重建项目,然后将更改保存到新数组(db和datasource)

onCollapse(rowID: number) {
    console.log("rowID", rowID);
    var newArray = this.state.db.slice();
    newArray[rowID] = {
        key: newArray[rowID].key,
        details: newArray[rowID].details,
        isCollapsed: newArray[rowID].isCollapsed == false ? true : false,
    };
    this.setState({
        dataSource: this.state.dataSource.cloneWithRows(newArray),
        db: newArray,
    });
}

答案 2 :(得分:3)

react足够聪明,可以检测dataSource中的更改以及是否应该重新呈现列表。如果要更新listView,请创建新对象,而不是更新现有对象的属性。 代码看起来像这样:

let newArray = this._rows.slice();
newArray[rowID] = {
  ...this._rows[rowID],
  Selection: !this._rows[rowID].Selection,
};
this._rows = newArray;

let newDataSource = this.ds.cloneWithRows(newArray);
this.setState({
  dataSource: newDataSource
});

您可以阅读有关类似issue on Github

的更多信息