我的redux状态看起来像这样(与Firebase同步)。

{
  profile: {
    activeUsers: {
      Iiva2BGZffNTH84glOLXv8QHVTF2: {
        sex: male,
        age: 20,
      },
      PkfMxrN09RN7ygoBMWqm4jheEOx1: {
        sex: female,
        age: 20,
      },
      zQiGXvcUGmRSKUdr719621QleUw2: {
        sex: male,
        age: 25,
      }
    }
  }
}


我要删除用户zQiGXvcUGmRSKUdr719621QleUw2

这是我的动作创作者

  Firebase.database()
    .ref('profiles/activeUsers')
    .on(
      'child_removed',
      (snapshot) => {
        dispatch(_activeUserChildRemoved(snapshot));
      },
      (err) => {
        console.log(err.toString());
        Alert.alert(err);
      },
    );
};

const _activeUserChildRemoved = snapshot => ({
  type: ACTIVE_USER_CHILD_REMOVED,
  payload: snapshot,
});


最后是我的减速器

  switch (action.type) {
    case ACTIVE_USER_CHILD_REMOVED:
      const key4Del = action.payload.key;
      return { //what goes here??? };
    default:
      return state;
  }
};


为了从redux中删除snapshot.key引用的用户,我从reducer返回什么?
非常感谢帮助

最佳答案

得到它了!!

case ACTIVE_USER_CHILD_REMOVED:
      const key4Del = action.payload.key;
      const oldState = state;
      delete oldState.activeUsers[key4Del];
      return { ...oldState };

关于react-native - 在触发Firebase'child_removed'监听器后,将嵌套的子级从redux状态中删除,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54662014/

10-11 13:08