我想将状态值的一些详细信息保存到firestore中。我该如何实现?
state = {
sp_Name: '',
sp_Phone: '',
sp_email: '',
sp_Role: 'Fleet Provider',
usr_org_LicenseNumber: '',
sp_License:'',
usr_org_StateConvered: '',
usr_org_DistConvered: '',
EquipmentCount: 0,
loading:false,
checked: false,
disChecked : false,
open: false,
message:'',
sp_NameError: '',
sp_PhoneError: '',
sp_emailError: '',
usr_org_LicenseNumberError: '',
sp_LicenseError:'',
usr_org_StateConveredError: '',
usr_org_DistConveredError: '',
sp_NumberofEquipmentsError:'',
};
我要删除一些细节,但是还有其他方法可以一次删除所有细节。
const stateObj = this.state;
delete stateObj['loading'];
delete stateObj['checked'];
delete stateObj['disChecked'];
delete stateObj['open'];
delete stateObj['message'];
delete stateObj['sp_NameError','sp_PhoneError','sp_emailError'];
delete stateObj['usr_org_LicenseNumberError', 'sp_LicenseError','usr_org_StateConveredError',
'usr_org_DistConveredError','sp_NumberofEquipmentsError'];
this.props.UpdateUserDetails(uid, stateObj)
this.openSnackbar({ message: 'Submitted Successfully.!' });
答案 0 :(得分:0)
您可以使用Object.entries
和array.reduce
的组合,这应该使它更简洁一些:
您只需要一个包含不再需要的键的数组:
let keysToRemove = ["loading", "checked", "disChecked", "open", "message", "sp_NameError", "sp_PhoneError", "sp_emailError", 'usr_org_LicenseNumberError', 'sp_LicenseError','usr_org_StateConveredError','usr_org_DistConveredError','sp_NumberofEquipmentsError']
和一个新状态
let newState = Object.entries({...state}).reduce((obj, [key, value]) => {
if(!keysToRemove.includes(key)){
obj[key] = value
}
return obj
}, {})
this.props.UpdateUserDetails(uid, newState)
答案 1 :(得分:0)
尝试这个。
keysToBeRemoved = ["loading", "checked", "disChecked", "open", "message"];
this.setState(prevState => ({
...Object.keys(prevState).filter(item => !keysToBeRemoved.includes(item)
).map(field => ({
[field]: prevState[field]}
))
}))