正在redux状态数组中设置特定索引



这基本上就是我想要做的。问题是这不会更新状态。知道我错过了什么吗?

type Tree = Array<Element>;
type SetLayerTreeItem = { payload: Element };
const initialState: Tree = [];
const LayersTree = createSlice({
name: 'LayersTree',
initialState,
reducers: {
setLayerTreeItem(state, { payload }: SetLayerTreeItem) {
state[payload.level] = payload; // Here's the problem
},
},
});

我最终做了这个:

setLayerTreeItem(state, { payload }: SetLayerTreeItem) {
const newState = state.slice();
newState[payload.level] = payload.element;
return newState;
},

相关内容

  • 没有找到相关文章

最新更新