[英]React-Native ListView not loading data correctly
I have a list view which contains a list of strings. 我有一个包含字符串列表的列表视图。 Initially the array from which it gets its rows from should be empty, but just to test it I have provided data in it.
最初,从中获取行的数组应该为空,但是为了进行测试,我已经在其中提供了数据。 However it does not show up.
但是它没有显示。 It only shows when call lapPressed which is intended to add a row to the table with a new string.
仅在调用lapPressed时显示,该调用旨在将带有新字符串的行添加到表中。 And the new string only gets added once I press the button again.
而且,只有当我再次按下按钮时,才会添加新的字符串。 This loops and the newest data only gets added upon calling the lapPressed again.
这样循环,仅在再次调用lapPressed时才添加最新数据。
To recap: 回顾一下:
I have removed the irrelevant code, Here is my code: 我删除了不相关的代码,这是我的代码:
constructor(props) {
super(props);
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
//dataSource: this.state.dataSource.cloneWithRows(this.state.laps)
this.state = {
laps: [2,3],
dataSource: ds.cloneWithRows(this.laps),
}
}
render() {
return <View>
{this.showList()}
</View>
}
showList() {
return <ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
/>
}
renderRow(rowData, sectionID, rowID) {
return (
<View style={styles.row}>
<Text style={styles.rowText} numberOfLines={1}>{rowData}</Text>
</View>
);
}
lapPressed = () => {
var lap = formatTime(this.state.timeElapsed);
this.setState({
startTime: new Date(),
laps: this.state.laps.concat([lap]),
dataSource: this.state.dataSource.cloneWithRows(this.state.laps)
});
return
}
This is your problem: 这是你的问题:
this.state = {
laps: [2,3],
dataSource: ds.cloneWithRows(this.laps),
}
this.laps
is not defined. this.laps
。 It should be this.state.laps
, but that's not assigned here either. 应该是
this.state.laps
,但是这里也没有分配。 I think this is what you need: 我认为这是您需要的:
var laps = [2, 3];
this.state = {
laps: laps,
dataSource: ds.cloneWithRows(laps),
}
Well it's because I was providing the old state in the lapPressed, this is the correct version. 那是因为我在lapPressed中提供了旧状态,所以这是正确的版本。
lapPressed = () => {
var lap = formatTime(this.state.timeElapsed);
var temp = this.state.laps.concat([lap])
this.setState({
startTime: new Date(),
laps: temp,
dataSource: this.state.dataSource.cloneWithRows(temp)
});
return
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.