我的redux状态看起来像这样(与Firebase同步)。
@IBAction func settingsExitButtonPressed(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
// This is used to let the Root View know to only reload itself
// when coming from the settings.
didOpenFromSettings = true
}
我要删除用户zQiGXvcUGmRSKUdr719621QleUw2
这里是我的动作创建者
{
profile: {
activeUsers: {
Iiva2BGZffNTH84glOLXv8QHVTF2: {
sex: male,
age: 20,
},
PkfMxrN09RN7ygoBMWqm4jheEOx1: {
sex: female,
age: 20,
},
zQiGXvcUGmRSKUdr719621QleUw2: {
sex: male,
age: 25,
}
}
}
}
最后是我的减速器
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,
});
为了从redux中删除snapshot.key引用的用户,我应该从reducer返回什么? 非常感谢帮助
答案 0 :(得分:2)
知道了!!
case ACTIVE_USER_CHILD_REMOVED:
const key4Del = action.payload.key;
const oldState = state;
delete oldState.activeUsers[key4Del];
return { ...oldState };
答案 1 :(得分:0)
您需要通过不进行变异状态,而只需使用解构来删除用户。
现在,您要通过从state
删除对象属性来直接对oldState
进行突变
case ACTIVE_USER_CHILD_REMOVED:
const key4Del = action.payload.key;
const { [key4Del]: _, ...activeUsers} = state.activeUsers
return {...state, activeUsers }
“扩展语法”将浅比较对象,因此您需要再次合并已过滤的对象以生成新状态。