Javascript 反应本机ListView未正确加载数据
我有一个包含字符串列表的列表视图。最初,它从中获取行的数组应该是空的,但为了测试它,我在其中提供了数据。然而,它并没有出现。它仅在按下call lapPressed时显示,call lapPressed旨在使用新字符串向表中添加行。新的字符串只有在我再次按下按钮时才会被添加。这个循环和最新的数据只会在再次调用lappress时添加 重述:Javascript 反应本机ListView未正确加载数据,javascript,reactjs,react-native,ecmascript-6,Javascript,Reactjs,React Native,Ecmascript 6,我有一个包含字符串列表的列表视图。最初,它从中获取行的数组应该是空的,但为了测试它,我在其中提供了数据。然而,它并没有出现。它仅在按下call lapPressed时显示,call lapPressed旨在使用新字符串向表中添加行。新的字符串只有在我再次按下按钮时才会被添加。这个循环和最新的数据只会在再次调用lappress时添加 重述: 在调用lapPressed之前,数组laps的初始元素不会显示 新行在被调用两次后才会显示 我已经删除了不相关的代码, 这是我的密码: constru
- 在调用lapPressed之前,数组laps的初始元素不会显示
- 新行在被调用两次后才会显示
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
}
构造函数(道具){
超级(道具);
var ds=新的ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2});
//dataSource:this.state.dataSource.cloneWithRows(this.state.laps)
此.state={
圈数:[2,3],
数据源:ds.cloneWithRows(this.laps),
}
}
render(){
返回
{this.showList()}
}
showList(){
返回
}
renderRow(行数据、节ID、行ID){
返回(
{rowData}
);
}
lapPressed=()=>{
var lap=格式化时间(this.state.timeappeased);
这是我的国家({
开始时间:新日期(),
圈数:此.state.laps.concat([lap]),
dataSource:this.state.dataSource.cloneWithRows(this.state.laps)
});
返回
}
这是您的问题:
this.state = {
laps: [2,3],
dataSource: ds.cloneWithRows(this.laps),
}
未定义此.laps
。它应该是this.state.laps
,但这里也没有指定它。我想这就是你需要的:
var laps = [2, 3];
this.state = {
laps: laps,
dataSource: ds.cloneWithRows(laps),
}
这是因为我在Lappress中提供了旧状态,这是正确的版本
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
}
这使得第一个元素在启动时出现,但第二个问题仍然存在。当我调用lapPressed时,没有任何更新,当我再次按下它时,它会更新,它总是落后一个。同样的问题。您的
cloneWithRows
调用获取上一圈的值this.state.laps
尚未更新。谢谢,我刚刚同时发现:)