繁体   English   中英

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

[英]React-Native Updating List View DataSource

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

有任何想法吗?

试试这个:

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中的原始数组进行this.state.ds

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

基本上,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,
    });
}

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
});

您可以在Github上阅读有关类似问题的更多信息

暂无
暂无

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

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