Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 为什么我可以添加元素,但不能从集合中删除_React Native - Fatal编程技术网

React native 为什么我可以添加元素,但不能从集合中删除

React native 为什么我可以添加元素,但不能从集合中删除,react-native,React Native,我正在尝试更新数据库中的通知计数 我通过创建一个集合来实现这一点,当我想添加到通知计数时,我会在集合中添加一个UID,当我想从通知计数中减去时,会从集合中删除一个UID 然后,我获取集合的大小并更新通知计数 updateNotificationCount函数由低阶组件触发 但是,我只能在isNewMatch为true时更新数据库。为什么当isNewMatch为false时它不更新数据库 state = {notificationSet: new Set()} updateNotificati

我正在尝试更新数据库中的通知计数

我通过创建一个集合来实现这一点,当我想添加到通知计数时,我会在集合中添加一个UID,当我想从通知计数中减去时,会从集合中删除一个UID

然后,我获取集合的大小并更新通知计数

updateNotificationCount函数由低阶组件触发

但是,我只能在isNewMatch为true时更新数据库。为什么当isNewMatch为false时它不更新数据库

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
    });
  };
}