3 回答

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

TA貢獻2041條經(jīng)驗 獲得超4個贊
對于那些使用 React/Redux 遇到此錯誤消息的人,可能是您試圖直接改變狀態(tài),這是不允許的。
就我而言,我有這樣的設(shè)置來獲取 thunk 中的狀態(tài)(簡化):
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);
這是通過以下方式為我解決的:
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貢獻1846條經(jīng)驗 獲得超7個贊
我在做一個nextjs項目時遇到了這個確切的錯誤。當(dāng)我收到此錯誤時,我將 a 放在findIndex一個對象數(shù)組上,嘗試將新的鍵值對添加到數(shù)組的特定對象中。所以我只是這樣做了:
const arrayOfObjects = [...someOtheObj.originalArrayKey]
const index = arrayOfObjects.findIndex((obj)=>{
// I had some conditions here
})
arrayOfObjects[index] = newValue
正確的
const arrayOfObjects = [...someOtheObj.originalArrayKey]
錯誤的
const arrayOfObjects = someOtheObj.originalArrayKey
添加回答
舉報