到目前为止,我一直将所有内容存储在数组中,但是遇到有关键对象索引以及遍历数组如何导致性能下降的StachOverflow问题之后,我想转向将内容存储为对象。但是我不太了解语法。
该reducer用于创建索引对象,但似乎不起作用。如何修复它以在下面产生所需的对象形状?
type MsgPayload = {
type: string;
msgKey?: string;
index?: number;
};
type IndexedMsgPayload = {
[key: number]: MsgPayload;
};
const messengerSlice = createSlice({
name: "messages",
initialState,
reducers: {
emitMessage: (state, action: PayloadAction<MsgPayload | any>) => {
state.total++;
const indexedObj: IndexedMsgPayload = {
0: {
...action.payload,
},
};
action.payload[state.total] = indexedObj[0];
state.messages = { ...state.messages, ...action.payload[state.total] };
},
},
});
我想实现以下目标:
{
1: {
type: 'type',
msgKey: 'alert'
},
}
Redux Toolkit已经拥有一个createEntityAdapter
API,可以为您完成定义和更新规范化状态结构的工作:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句