angular - 为什么一个集合不触发存储更改?
问题描述
我的店铺是这样的
export interface State extends EntityState<featureModels.IModel> {
selectedModelsId: Set<number>;
isLoading?: boolean;
error?: any;
}
我有以下行动:
export class SelectModels implements Action {
public readonly type = ActionTypes.SelectModels;
constructor(public payload: {modelsIds: Array<number>}) {}
}
并且动作触发了这个减速器
const SELECT_MODELS = (state: State, action: featureAction.SelectModels) => {
console.log(new Set([...Array.from(state.selectedModelsId), ...action.payload.modelsIds]));
return {
...state,
selectedModelsId: new Set([...Array.from(state.selectedModelsId), ...action.payload.modelsIds]),
};
};
动作被正确调度,日志很好,它显示了我想要的。
但是在 redux 检查器中,我看到状态没有变化。但它实际上应该改变,因为我应该有一个带有一些值的集合而不是一个空集合(如 console.log 显示)
为什么 ?
编辑1:
我尝试了一个简单的:
return {
...state,
selectedModelsId: 1234,
};
但
return {
...state,
selectedModelsId: new Set(3),
};
才不是
编辑 2:
const SELECT_MODELS = (state: State, action: featureAction.SelectModels) => {
console.log(new Set([...Array.from(state.selectedModelsId), ...action.payload.modelsIds]));
console.log({
...state,
selectedModelsId: new Set([...Array.from(state.selectedModelsId), ...action.payload.modelsIds])
});
return {
...state,
selectedModelsId: new Set([...Array.from(state.selectedModelsId), ...action.payload.modelsIds])
};
};
即使这样,日志也是正确的 a selectedModelsId: Set(1) {3}
,但在 redux 中有(states are equal)
编辑 3:
我将所有内容都更改为数组,现在它可以工作了......但是为什么它不能与 set 一起工作?
解决方案
ASet
不可序列化,因此 devtools 无法正确读取它。数组是可序列化的,开发工具可以读取这些值。
推荐阅读
- node.js - 我们可以在 ChartsJS 中使用 EJS 标签吗?
- chart.js - 为什么条形饼图值不对齐?
- swiftui - SwiftUi - 阻止更新
- c++ - 2D数组C ++的分段错误错误
- javascript - 捕获计数器的值 - 功能范围
- python - Django 中的表单不会在数据库中注册数据
- r - 使用平均值聚合每日数据
- javascript - 动态 React 路由器和组件不可能?
- reactjs - 为什么只有在上传文件时才更新错误的元素?
- kubernetes - 如何在 terraform 中使用 nginx 入口为 kubernetes 创建路由 - scaleway