javascript - Redux 状态 - 只有有时它显示“状态相等”
问题描述
我将 ReactJS 与 Redux 一起使用,并且在 redux 更新时遇到了问题。
在一个组件中,我有以下内容:
import { connect } from "react-redux";
import { Dispatch } from "redux";
import { State } from "state/State";
import { FormActions } from "state/form/actions";
import { StateToProps, DispatchToProps, DialogMetaComponent } from "./types";
import hasResponseId from "utils/hasResponseId";
import PrintDialog, { COMMAND_RESPONSE_ID } from "./PrintDialog";
const mapDispatchToProps = (
dispatch: Dispatch,
{ metaComponent }: { metaComponent: DialogMetaComponent },
): DispatchToProps => ({
onCleanUpCancel: () => {
const { cancelCommands }: DialogMetaComponent = metaComponent;
if (
!cancelCommands ||
(Array.isArray(cancelCommands) && !cancelCommands.length)
) {
return;
}
cancelCommands.map((command) =>
dispatch(
FormActions.removeCommand({
id: command,
}),
),
);
},
});
export default connect(mapStateToProps, mapDispatchToProps)(PrintDialog);
那个行动:
export interface FormCommand {
id: string;
forResponseId?: string;
params?: {
[key: string]: any;
};
[key: string]: any;
}
export const FormActions = {
removeCommand: (command: FormCommand) =>
action(FormActionTypes.REMOVE_COMMAND, command),
}
这是对应的reducer:
case FormActionTypes.REMOVE_COMMAND:
{
const commands = state.commands.filter(
(command: FormCommand) =>
!command.forResponseId ||
!command.forResponseId.includes(action.payload.id),
);
return {
...state,
commands: [...commands],
};
}
大多数情况下,它会正确删除命令。但有时 redux 会说:状态是平等的。并且不会删除任何命令。
通常我会期望:
FormActions.removeCommand({
id: command,
})
总是创建一个新对象(带有一个新的引用)。
你有想法吗 ?
解决方案
推荐阅读
- visual-studio - 在 VS 2019 中部署驱动程序失败
- triggers - trigger on opportunity whenever the data of the field get changed in the Custommetadata, then update that field data on the related Account field
- python - 从字典中查找python中最大的3个数字
- r - 我如何在 R 中表示这个几何级数?
- python - 如何在与子进程相同的目录中使用python调用脚本?
- javascript - 创建了一个罐头幻灯片(JS 和 JQuery),但移动罐头有困难
- css - 如果内部跨度文本为空,则通过 CSS 隐藏锚标记
- swift - AVAudioPlayer.play() 有效,但 AVAudioPlayerNode.play() 失败
- c++ - 移位运算符如何在c ++中处理负数
- arrays - 过滤数组以获取多个值并限制结果