我有这个简化的代码
class Request extends Component {
constructor(props){
super(props);
this.Remove = this.Remove.bind(this)
}
componentWillMount(){
firebase.database().ref('/Users/' + AuthID).orderByChild('UserRequests').on("value", function(snapshot) {
snapshot.forEach((child) => {
Name = child.val().Name;
UserID = child.val().UserID;
obj = {key: keyIndex++, Name: Name, ID: UserID};
Requests.push(obj)
});
self.setState({Array: Requests});
});
}
//Need to call map func again as soon as user use Remove func and remove data
RenderUsers = () => {
return this.state.Array.map((Data) => (
<View key={Data.key}>
<TouchableHighlight onPress={() => {
this.Remove(Data.ID, Data.Name, Data.key);
}}>
<Text> Remove </Text>
</TouchableHighlight>
</View>
))
};
Remove = (ID, Name, KeyIndex) => {
this.state.Array.splice(KeyIndex, 1);
firebase.database().ref('Users/' + AuthID + '/UserRequests/').child(ID).remove();
//this.RenderUsers();
};
render(){
<View>
{this.RenderUsers()}
</View>
}
}
假设地图功能渲染了10个视图。用户点击TouchableHighlight
后,会立即从Firebase右侧View
(请求)中删除它,并且它不再存在。不幸的是,删除数据后我无法重新映射数组。我尝试了很多方法但没有任何效果。我认为问题是我在componentWillMount中获取数据。因此,在第一次安装后无法从Firebase获取数据,但如果我在该屏幕上运行应用程序时向数据库添加请求,则会立即显示。问题不应该在componentWillMount函数中。