gpt4 book ai didi

react-native - 在 Firebase 'child_removed' 监听器触发后从 redux 状态中删除嵌套子级

转载 作者:行者123 更新时间:2023-12-04 04:10:18 24 4
gpt4 key购买 nike

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

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

我想删除用户 zQiGXvcUGmRSKUdr719621QleUw2

这是我的 Action 创作者

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

最后是我的reducer

  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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com