在Redux Actions中从cuid获取模型

时间:2018-10-18 13:58:06

标签: reactjs redux

我去了Redux Actions文件:tributionActions

我的功能是:

  export function acceptContribution(contributionCuid) {
  return dispatch => {
    const {
      //How can I construct contribution from  contributionCuid
      contriution: { activity } } = contributionCuid;
    dispatch(actionStart(CONTRIBUTIONS_TO_VALIDATE_REMOVE));
    Mixpanel.track('acceptContribution2', {
      'Mission': contribution.activity.name,
      'Charity': contribution.activity.charity.name,
    });
    return callApi(`contributions/${contributionCuid}/accept`, 'post')
      .then(() => dispatch(removeContributionToValidateAction(contributionCuid)))
      .catch(err => dispatch(actionError(CONTRIBUTIONS_TO_VALIDATE_REMOVE, err)));
  };
}

现在,我想将有关此贡献的信息发送给Mixpanel。因此,我需要重构贡献,以便获得所需的全部价值。 有可能吗?

1 个答案:

答案 0 :(得分:0)

也许您不应该解构从参数中获得的贡献。

我已经修改了您的代码:

export function acceptContribution(contributionCuid) {
  return dispatch => {
    // do not deconstruct the contribution object, so we keep a reference to it -- is it really needed?
    const { contribution } = contributionCuid;
    // you get the activity here
    const { activity } = contribution;

    dispatch(actionStart(CONTRIBUTIONS_TO_VALIDATE_REMOVE));
    Mixpanel.track('acceptContribution2', {
      'Mission': activity.name,
      'Charity': activity.charity.name,
    });
    return callApi(`contributions/${contributionCuid}/accept`, 'post')
      .then(() => dispatch(removeContributionToValidateAction(contributionCuid)))
      .catch(err => dispatch(actionError(CONTRIBUTIONS_TO_VALIDATE_REMOVE, err)));
  };
}

您真的要从减速器动作中调用该Mixpanel函数吗?我希望让我的减速器专门处理该问题。