我首先应该说这不是重复的这个问题,恰好有相同的标题 https://stackoverflow.com/questions/48648736/redux-state-is-being-edited-without-dispatching-any-action.
我只是得到一个customers
数组对象来自props
里面一个componentDidMount
像这样的方法;
componentDidMount() {
const { customers } = this.props
const expiringCustomers = getExpiringCustomers(customers)
console.log('Expiring customers ', expiringCustomers)
}
在另一个文件中,我有getExpiringCustomers
该函数接受传递的客户并返回新修改的客户列表,如下所示;
function numbersOnly(value) {
if(_.isString(value)) {
value = Number(value.replace(/[^\d]/g, ''))
}
return value
}
function normalizeNumber(collection, field) {
return collection.map(obj => {
obj[field] = numbersOnly(obj[field])
return obj
})
}
export function getExpiringCustomers(customers) {
const expiringCustomers = customers.filter(customer => {
const daysLeft = Number(new Date(customer.endDate)) - _.now()
if(daysLeft <= (dateInMonth * 3)) {
return customer
}
})
return normalizeNumber(expiringCustomers, 'rent')
}
我将我的反应组件与 redux 状态连接起来,如下所示;
const mapStateToProps = state => ({
customers: state.customers.filter(customer => customer && !customer.deleted)
})
export default connect(mapStateToProps)(Accounting)
Problem
函数运行并记录结果后,客户的状态在 redux 存储中发生更改。
这很令人困惑,因为customers_edit
操作必须经过一些过程,但没有一个过程被调用/记录。
受影响对象的快照:
Ps.数据只是样板。
你改变了对象normalizeNumber
,因为您使用的所有数组方法都不会克隆数组的对象。
Change normalizeNumber
回调以返回具有更新字段的新对象:
function normalizeNumber(collection, field) {
return collection.map(obj => ({
...obj,
[field]: numbersOnly(obj[field])
}))
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)