【问题标题】:Redux state is being updated without dispatching any actionRedux 状态正在更新而不调度任何操作
【发布时间】:2018-07-10 09:11:56
【问题描述】:

首先我应该说这不是 this question, which happened to have the same title 的重复。

我只是在这样的componentDidMount 方法中从props 获取数组的customers 对象;

  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')
}

我正在将我的 react 组件与这样的 redux 状态连接起来;

const mapStateToProps = state => ({
    customers: state.customers.filter(customer => customer && !customer.deleted)
})

export default connect(mapStateToProps)(Accounting)

问题

函数运行并记录结果后,redux store 中客户的状态发生变化。

这非常令人困惑,因为 customers_edit 操作必须通过一些过程,但没有一个被调用/记录。

受影响对象的快照:

附言。数据只是样板。

//- Focus on rent property

const customers = [
...,
{
      id: 'o91wukyfsq36qidkld02a0voo93rna5w',
      cardId: 'GD-1101010111',
      id_type: 'Driving License',
      firstName: 'Maalim',
      lastName: 'Guruguja',
      names: 'Maalim Guruguja',
      property: '5iaprurefg3v3uhad688mypo9kqf6xk3',
      rent: '250,000',
      email: 'tonimarikapi@yahoo.com',
      phone: '239-288-3838-38',
      noticePeriod: '3',
      status: '2 months remain',
      startDate: '2018-07-09',
      endDate: '2018-08-17',
      createdAt: 1530623480772,
      updatedAt: 1531213159147
    },
    ...
]

//- After the functions run, log and edit customers array
const customers = [
...,
{
      id: 'o91wukyfsq36qidkld02a0voo93rna5w',
      cardId: 'GD-1101010111',
      id_type: 'Driving License',
      firstName: 'Maalim',
      lastName: 'Guruguja',
      names: 'Maalim Guruguja',
      property: '5iaprurefg3v3uhad688mypo9kqf6xk3',
      rent: 250000,
      email: 'tonimarikapi@yahoo.com',
      phone: '239-288-3838-38',
      noticePeriod: '3',
      status: '2 months remain',
      startDate: '2018-07-09',
      endDate: '2018-08-17',
      createdAt: 1530623480772,
      updatedAt: 1531213159147
    },
    ...
]

从链接的问题(可能重复的问题)中,回答的人说可能是一些突变问题导致了这种情况。我不确定这是否适用于假定为只读的道具。

如何阻止这些函数更新我的 redux 商店,请帮忙。

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    您更改了normalizeNumber 中的对象,因为您使用的所有数组方法都不会克隆数组的对象。

    更改normalizeNumber 回调以返回具有更新字段的新对象:

    function normalizeNumber(collection, field) {
        return collection.map(obj => ({
            ...obj,
            [field]: numbersOnly(obj[field])
        }))
    }
    

    【讨论】:

    • 哈哈,我笑了,对自己说,“编程我的朋友,这叫编程”。谢谢,成功了!
    【解决方案2】:

    您似乎无意中修改了customers 数组。

    试试:

    componentDidMount() {
          const { customers } = { ...this.props };
          const expiringCustomers = getExpiringCustomers(customers)
          console.log('Expiring customers ', expiringCustomers)
      }
    

    【讨论】:

    • 我刚刚尝试了您的解决方案,结果仍然相同。客户状态仍然发生变化。
    • 我什至试过const customers = this.props.customers还是同样的问题。
    猜你喜欢
    • 2019-12-04
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2019-08-15
    • 2016-12-18
    • 2021-03-29
    • 1970-01-01
    相关资源
    最近更新 更多