React native 为什么我可以添加元素,但不能从集合中删除
我正在尝试更新数据库中的通知计数 我通过创建一个集合来实现这一点,当我想添加到通知计数时,我会在集合中添加一个UID,当我想从通知计数中减去时,会从集合中删除一个UID 然后,我获取集合的大小并更新通知计数 updateNotificationCount函数由低阶组件触发 但是,我只能在isNewMatch为true时更新数据库。为什么当isNewMatch为false时它不更新数据库React native 为什么我可以添加元素,但不能从集合中删除,react-native,React Native,我正在尝试更新数据库中的通知计数 我通过创建一个集合来实现这一点,当我想添加到通知计数时,我会在集合中添加一个UID,当我想从通知计数中减去时,会从集合中删除一个UID 然后,我获取集合的大小并更新通知计数 updateNotificationCount函数由低阶组件触发 但是,我只能在isNewMatch为true时更新数据库。为什么当isNewMatch为false时它不更新数据库 state = {notificationSet: new Set()} updateNotificati
state = {notificationSet: new Set()}
updateNotificationCount = (uid, isNewMatch) => {
if (isNewMatch) {
this.setState(({ notificationSet }) => ({
notificationSet: new Set(notificationSet).add(uid)
}));
}
else {
this.setState(({ notificationSet }) => {
const newNotificationSet = new Set(notificationSet);
newNotificationSet.delete(uid);
return {
notificationSet: newNotificationSet
};
});
};
}
您不必每次都执行
new Set()
,因为您已经使用new Set()
初始化了状态,所以现在只需执行以下操作:
state = {notificationSet: new Set()}
updateNotificationCount = (uid, isNewMatch) => {
let notificationSet;
if (isNewMatch) {
notificationSet=this.state.notificationSet;
notificationSet.add(uid);
this.setState({
notificationSet: notificationSet
});
} else {
notificationSet=this.state.notificationSet;
notificationSet.delete(uid);
this.setState({
notificationSet : notificationSet
});
};
}