在 immer 中使用 produce 更新数组值

Update the array values using produce in immer

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

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] 未定义

的错误

好吧,草稿是 customers 自己的 clone,因此不包括客户, 试试这个:

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