问题描述
我正在尝试使用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;
}
});