React native ListView正在删除错误的项
我认为这可能是一个重复的问题,但在我看来一切都是正确的。我的函数确实删除了一个项,但它删除了错误的项。当我使用console.log检查对象数组时,我选择的项不再存在。使用新对象,然后设置状态。这是我的密码:React native ListView正在删除错误的项,react-native,React Native,我认为这可能是一个重复的问题,但在我看来一切都是正确的。我的函数确实删除了一个项,但它删除了错误的项。当我使用console.log检查对象数组时,我选择的项不再存在。使用新对象,然后设置状态。这是我的密码: export default class MainApp extends Component { constructor(props) { super(props); this.state = {
export default class MainApp extends Component {
constructor(props) {
super(props);
this.state = {
damping: 1 - 0.6,
tension: 400,
dataBlob: [],
dataSource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}),
};
}
componentDidMount() {
fetch('http://54.152.253.14/barbershop/appointmentHelper.php')
.then((response) => response.json())
.then((responseData) => {
this.state.dataBlob = responseData;
this.setState({
dataSource:this.state.dataSource.cloneWithRows(this.state.dataBlob),
isLoading: false,
});
})
.done();
}
deleteItem(rowId){
console.log(rowId);
this.state.dataBlob.splice(rowId,1);
console.log(this.state.dataBlob);
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.state.dataBlob),
})
}
fetchData() {
}
renderList = (dataObj,sectionID,rowId) => {
return (
<View style={styles.container}>
<Row damping={this.state.damping} tension={this.state.tension} onPress={()=>this.deleteItem(rowId)}>
<View style={styles.rowContent}>
<View style={styles.rowIcon} />
<View>
<Text style={styles.rowTitle}>{dataObj.service}</Text>
<Text style={styles.rowSubtitle}>Drag the row left and right</Text>
</View>
</View>
</Row>
</View>
);
};
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(data, sectionID, rowID) => this.renderList(data, sectionID, rowID)}
style={styles.listView}
/>
);
}
}
导出默认类MainApp扩展组件{
建造师(道具){
超级(道具);
此.state={
阻尼:1-0.6,
张力:400,
dataBlob:[],
dataSource:new ListView.dataSource({rowHasChanged:(r1,r2)=>r1!==r2}),
};
}
componentDidMount(){
取('http://54.152.253.14/barbershop/appointmentHelper.php')
.then((response)=>response.json())
.然后((响应数据)=>{
this.state.dataBlob=响应数据;
这是我的国家({
dataSource:this.state.dataSource.cloneWithRows(this.state.dataBlob),
孤岛加载:false,
});
})
.完成();
}
删除项(rowId){
console.log(rowId);
this.state.dataBlob.splice(rowId,1);
log(this.state.dataBlob);
这是我的国家({
dataSource:this.state.dataSource.cloneWithRows(this.state.dataBlob),
})
}
fetchData(){
}
renderList=(dataObj,sectionID,rowId)=>{
返回(
this.deleteItem(rowId)}>
{dataObj.service}
左右拖动该行
);
};
render(){
返回(
this.renderList(数据,节ID,行ID)}
style={styles.listView}
/>
);
}
}
任何帮助都将不胜感激。我觉得我缺少了一些关于列表视图的概念性想法,我真的很期待提高我的理解力。谢谢。将我的渲染功能更改为此解决了我的问题:
render() {
return (
<ListView
key={this.state.dataBlob}
dataSource={this.state.dataSource}
renderRow={(data, sectionID, rowID) => this.renderList(data, sectionID, rowID)}
style={styles.listView}
/>
);
}
render(){
返回(
this.renderList(数据,节ID,行ID)}
style={styles.listView}
/>
);
}
基本上,添加一个键来跟踪列表中的唯一项是我的救命稻草。希望这能在将来为其他人提供帮助