gpt4 book ai didi

listview - React Native ListView 不会在数据更改时更新

转载 作者:行者123 更新时间:2023-12-03 14:43:30 25 4
gpt4 key购买 nike

我不确定为什么当我的数据更改时 ListView 没有更新。我已经将我的代码 strip 化以使其易于阅读并创建了一个 rnplay:

https://rnplay.org/apps/ivG0mg

我期望发生的是,用户单击列表项和行的 bool 值 isCollapsed切换使背景为红色。实际发生的是数据源已更新,但 ListView 无法识别更改并且未呈现任何新内容。有任何想法吗?

'use strict';

var React = require('react-native');

var {
ScrollView,
Text,
Image,
StyleSheet,
TouchableHighlight,
AppRegistry,
View,
ListView,
} = React;

var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
padding: 15,
},
red: {
backgroundColor: "red",
}
});

var foods = [
{key: 'Almond Milk (Homemade)', details:''},
{key: 'Club Soda', details:'', isCollapsed: true},
{key: 'Coconut Milk/Cream', details:''},
{key: 'Coconut Water', details:''},
{key: 'Coffee/Espresso', details:'', isCollapsed: true},
{key: 'Fruit Juice', details:''},
{key: 'Kombucha', details:''},
{key: 'Mineral Water', details:''},
{key: 'Unsweetened Tea', details:''},
{key: 'Water', details:''},
{key: 'Fruit Juice', details:''},
{key: 'Kombucha', details:''},
{key: 'Mineral Water', details:''},
{key: 'Unsweetened Tea', details:''},
{key: 'Water', details:''},
{key: 'Fruit Juice', details:''},
{key: 'Kombucha', details:''},
{key: 'Mineral Water', details:''},
{key: 'Unsweetened Tea', details:''},
{key: 'Water', details:''},
{key: 'Fruit Juice', details:''},
{key: 'Kombucha', details:''},
{key: 'Mineral Water', details:''},
{key: 'Unsweetened Tea', details:''},
{key: 'Water', details:''},
{key: 'Fruit Juice', details:''},
{key: 'Kombucha', details:''},
{key: 'Mineral Water', details:''},
{key: 'Unsweetened Tea', details:''},
{key: 'Water', details:''},
];

class SampleApp extends React.Component{
constructor(props){
super(props);
var ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
});
this.state = {
dataSource: ds.cloneWithRows(foods),
};
}
_renderRow(data, sectionID, rowID) {
return (
<TouchableHighlight
style={[
styles.container,
data.isCollapsed && styles.red]}
onPress={()=>this.onCollapse(rowID)}>
<Text>{data.key}</Text>
</TouchableHighlight>
);
}
onCollapse(rowID: number) {
console.log("rowID", rowID);
foods[rowID].isCollapsed = !foods[rowID].isCollapsed;
this.setState({dataSource: this.state.dataSource.cloneWithRows(foods)});
}
render() {
return(
<ListView
style={styles.subContainer}
dataSource={this.state.dataSource}
renderRow={this._renderRow.bind(this)}
initialListSize={15}/>
)
}
};

AppRegistry.registerComponent('SampleApp', () => SampleApp);

最佳答案

这是一个工作版本的链接:

https://rnplay.org/apps/GWoFWg

这些是我需要进行的更改以修复它:

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

和这个:
onCollapse(rowID: number) {
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,
});
}

关于listview - React Native ListView 不会在数据更改时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com