Aki*_*i J 1 reactjs redux react-redux redux-toolkit
伙计们,如何在 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 },
];
}
Run Code Online (Sandbox Code Playgroud)
这是错误的,我知道这一点,但我想知道如何向此 currentTimeOfServices 空数组添加/附加对象?
Redux Toolkit 内部使用 Immer,它可以让您使用正常的 JS 语法“改变”现有状态。所以,你所需要的只是:
state.currentTimeOfServices.push(newItem)
您所拥有的内容对于手写的不可变更新来说是有效的,但这里没有必要。
请参阅文档了解更多详细信息:
| 归档时间: |
|
| 查看次数: |
11243 次 |
| 最近记录: |