伙計們如何將物件附加到 redux 工具包中的空陣列?我currentTimeOfServices:[]
在我的切片中設定了一個
,然后我發送了一個帶有有效載荷的動作
dispatch(userActions.getCurrentTime({ startTime: startTime, name: item }));
在我的 getCurrenTtime 減速器中,我不明白如何附加這個專案。
getCurrentTime: (state, action) => {
state.currentTimeOfServices = [
...state.currentTimeOfServices,
{ startTime: action.payload.startTime, name: action.payload.name },
];
}
這是錯誤的,我知道,但我想知道如何將物件添加/附加到這個 currentTimeOfServices 空陣列?
uj5u.com熱心網友回復:
Redux Toolkit 內部使用了 Immer,它可以讓您使用正常的 JS 語法“改變”現有狀態。所以,你只需要:
state.currentTimeOfServices.push(newItem)
你在那里得到的東西對于手寫的不可變更新是有效的,但在這里沒有必要。
有關更多詳細資訊,請參閱檔案:
- https://redux.js.org/tutorials/fundamentals/part-8-modern-redux#immutable-updates-with-immer
- https://redux-toolkit.js.org/usage/immer-reducers
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/345877.html
上一篇:如何將此函式轉換為類組件,以便我可以使用渲染來呼叫回傳?
下一篇:光滑滑塊幻燈片內的中心專案
