组件未在 redux 状态更改时更新

Component not updating on redux state change

正如标题所说,我的组件通过 connect 函数连接到 redux 存储,不会触发特定情况下状态更改的更新,同时在其他情况下运行良好。

被修改状态的变量结构如下

state: {
  billDetails: {
    productType1: {...},
    productType2: {...},          
    productType3: {...},
    productType4: {
      accountId1 : [
        {invoiceNo:1, isChecked: true},
        {invoiceNo:2, isChecked: false}  
      ],
      accountId2: [...]
    }
  }
}

我正在尝试从一个成功发生的减速器中更改 ischecked 的错误,因为我可以看到它在 react-native 调试器中发生了变化,但是组件本身没有得到更新。

这是我针对该特定情况的 reducer 代码的代码

{
  const { accountId, invoiceNo, isChecked } = action.payload;
  const billDetails = result(state.billDetail, `${action.payload.productType}.${accountId}`, []);
  const billDetailsModified = billDetails.map((bill) => {
    if (bill.invoiceNo === invoiceNo) {
      bill.isChecked = isChecked;
    }
    return bill;
  });
  const newState = Object.assign({}, state);
  newState.billDetail[action.payload.productType][accountId] = billDetailsModified;
  return newState;
}

组件使用billDetail: result(state, 'billUsage.billDetail', {})

连接到mapStateToProps函数中的billdetailsobject

Object.assign 复制 属性 值,所以对象引用被复制,所以 state.billDetail 和 newState.billDetail 指的是同一个对象。因此,您正在改变原始状态,这使得状态成为 not updating.