这基本上就是我想要做的。问题是这不会更新状态。知道我错过了什么吗?
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;
},