简体   繁体   English

redux-thunk - 即使我使用了 mapDispatchToProps 也没有调度

[英]redux-thunk - not dispatching even though I used mapDispatchToProps

I have a weird problem with my redux action.我的 redux 操作有一个奇怪的问题。

I have two functions A(), B() inside the same component which call an action function ac() that uses a thunk to do async call to the server.我在同一个组件中有两个函数 A()、B(),它们调用一个动作 function ac(),它使用一个 thunk 对服务器进行异步调用。

I used mapDispatchToprops to map ac() to the component.我使用 mapDispatchToprops 到 map ac() 到组件。

When I invoke A() it works like a charm, the action is dispatched and state is updated.当我调用 A() 时,它就像一个魅力,动作被调度并更新 state。 But if I invoke B() which invokes the same ac() in the same way the action is not dispatched.但是,如果我调用 B(),它会以相同的方式调用相同的 ac(),则不会调度该操作。 It is as if I am calling a normal function so the thunk inside is not executed.就好像我在调用一个普通的 function 所以里面的 thunk 没有被执行。

Am I missing something here?我在这里错过了什么吗?

EDIT: generateAgreement() works but signAgreement() doesn't, even though they call the same action updateAgreements()编辑: generateAgreement() 有效,但 signAgreement() 无效,即使它们调用相同的操作 updateAgreements()

generateAgreement = async evt => {
    evt.preventDefault();

    const selectedAgreement = this.props.myAgreements[evt.target.id];

    this.props.updateAgreements(
      Object.assign(
        {},
        {
          myAgreements: this.props.myAgreements,
          selectedAgreement
        }
      ),
      this.props.user.token,
      "generate-agreement",
      {
        agreementId: selectedAgreement.id
      },
      actionTypes.GENERATE_AGREEMENT
    );
  };

  signAgreement = async evt => {
    evt.preventDefault();

    const selectedAgreement = this.props.myAgreements[evt.target.id];

    this.props.updateAgreements(
      Object.assign(
        {},
        {
          myAgreements: this.props.myAgreements,
          selectedAgreement
        }
      ),
      this.props.user.token,
      "sign-agreement",
      {
        agreementId: selectedAgreement.id
      },
      actionTypes.SIGN_AGREEMENT
    );
  };

I figured out the problem.我解决了这个问题。 This is one of the classic examples to avoid using object destructuring all over the place.这是避免在各处使用 object 解构的经典示例之一。

In my case, in my parent component 1 I imported from the actions.js file as follows:就我而言,在我的父组件 1 中,我从actions.js文件中导入如下:

Component_1.js组件_1.js

import { updateAgreements } from "../redux/actions.js"

...

render() {
    return <Child 
        updateAgreements={updateAgreements}
    />
}

const mapDispatchToProps = {
   updateAgreements
}
...

And in parent component 2 I have the following code which is the reason generateAgreement worked as expected:在父组件 2 中,我有以下代码,这就是 generateAgreement 按预期工作的原因:

Component_2.js组件_2.js

import { updateAgreements } from "../redux/actions.js"

...

render() {\
    const { updateAgreements } = this.props;
    return <Child 
        updateAgreements={updateAgreements}
    />
}

const mapDispatchToProps = {
   updateAgreements
}
...

Notice that in component_1 updateAgreements is taken from the import statement and in component_2 it is taken from the props.请注意,在 component_1 中, updateAgreements取自 import 语句,而在 component_2 中,它取自 props。

I have seen a lot of people warning about overusing destructuring now I see how it can cause subtle bugs like this.我看到很多人警告过度使用解构,现在我知道它是如何导致像这样的微妙错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM