我注意到在redux中设置和初始状态操作对象时,空数组返回undefined
,这使我无法访问.push()
或.length
等数组方法。
有没有办法阻止值从简化到未定义?
export const startAddCustomer = (customerData = {}) => {
return (dispatch) => {
const {
name = '',
age = 0,
address = {
address: '',
city: '',
state: ''
},
coats = [],
shoes = [],
phoneNumber = '',
dependants = [],
visits = [],
} = customerData;
const customer = {
name,
age,
address,
coats,
shoes,
phoneNumber,
dependants,
visits
};
database.ref('customers').push(customer).then((ref) => {
dispatch(addCustomer({
id: ref.key,
...customer
}))
})
}
};