使用Producer in immer更新数组值

问题描述

我正在尝试使用immer中的Produce更新数组值,但是草稿对我来说是未定义的。你能告诉我我在做什么错吗?

export const getCustomers = (customers) => (dispatch,getState) => { 
  const activeCustomers = produce(customers,(draft) => {
    for (let i = 0; i < customers.length; i += 1) {
      draft.customers[i].position= i;
    }
  });

//I keep getting that error that draft.customers[i] is undefined

我在做什么错。不断收到未定义draft.customers [i]的错误

解决方法

草稿本身是clone中的customers,因此不包括服装销售商, 试试这个:

export const getCustomers = (customers) => (dispatch,getState) => { 
  const activeCustomers = produce(customers,(draft) => {
    for (let i = 0; i < draft.length; i ++) {
      draft[i].position= i;
    }
  });

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...