【发布时间】:2021-09-05 05:29:27
【问题描述】:
我有一个初始状态,其中包含带有键号的词汇对象,每个键都有对象数组,在 React redux 中,我们使用传播并在 reducer 中更新我们的状态,但在 redux 工具包中,我卡住了,我想添加每个键的数组对象
const initialState: IWordListState = {
vocabulary: {
7:[
{}
]
},
};
export const wordSlice = createSlice({
name: 'words',
initialState,
reducers: {
addWord: (
state,
action: PayloadAction<{ step: number; newWord: WordState }>,
) => {
console.log(current(state));
const { step, newWord } = action.payload;
state.vocabulary[step] = [{ ...newWord }];
},
},
});
我想在调用 addWord 函数时添加另一个对象,我得到一个步骤号作为词汇对象的键,如下所示:
vocabulary: {
7:[
{
id:211,
....
},
{
id:212,
....
}
],
6:[
{
id:213,
....
]
},
【问题讨论】:
标签: javascript reactjs redux redux-toolkit