如何解决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,})
总是创建一个新对象(带有新的引用)。
你有什么想法吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。