3 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
在嘗試對(duì)數(shù)組進(jìn)行排序之前,請(qǐng)嘗試創(chuàng)建該數(shù)組的副本。就像使用展開(kāi)運(yùn)算符一樣。
arrayForSort = [...this.taskList]
然后排序后,您可以將其分配回該taskList
字段

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊
對(duì)于那些使用 React/Redux 遇到此錯(cuò)誤消息的人,可能是您試圖直接改變狀態(tài),這是不允許的。
就我而言,我有這樣的設(shè)置來(lái)獲取 thunk 中的狀態(tài)(簡(jiǎn)化):
import store from "./myStore";
const state = store.getState();
const getItems = state => state.user.items;
const items = getItems(state);
// ↓ this blew up as it was attempting to manipulate `state`
items.sort((a, b) => a.order - b.order);
這是通過(guò)以下方式為我解決的:
import store from "./myStore";
const state = store.getState();
const getItems = state => state.user.items;
// ↓ in my case items is an array, so I create a new array by spreading state here
const items = [...getItems(state)];
// ↓ which means we're not manipulating state, but just our `items` array alone
items.sort((a, b) => a.order - b.order);

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
我在做一個(gè)nextjs項(xiàng)目時(shí)遇到了這個(gè)確切的錯(cuò)誤。當(dāng)我收到此錯(cuò)誤時(shí),我將 a 放在findIndex一個(gè)對(duì)象數(shù)組上,嘗試將新的鍵值對(duì)添加到數(shù)組的特定對(duì)象中。所以我只是這樣做了:
const arrayOfObjects = [...someOtheObj.originalArrayKey]
const index = arrayOfObjects.findIndex((obj)=>{
// I had some conditions here
})
arrayOfObjects[index] = newValue
正確的
const arrayOfObjects = [...someOtheObj.originalArrayKey]
錯(cuò)誤的
const arrayOfObjects = someOtheObj.originalArrayKey
添加回答
舉報(bào)