从减速机返回2件东西

时间:2018-02-12 13:52:54

标签: javascript reactjs redux return

我正在创建一个足球联赛表,用户可以输入2个队的结果然后更新表。我有它的工作但它开始变得凌乱,因为我开了太多的动作。

我有这个案例:

case 'UPDATE_WINNER': {
  const result = workOutWinner(action.data.homeTeam, action.data.awayTeam)
  return state.map(team => team.name === result.winners.name
    ?
    { ...team,
      won: team.won + 1,
      points: team.points + 3,
      goalsScored: team.goalsScored + result.winners.goals,
      goalsAgainst: team.goalsAgainst + result.losers.goals
    }
    : team )
}

和这一个:

   case 'UPDATE_LOSER': {
      const result = workOutWinner(action.data.homeTeam, action.data.awayTeam)
      return state.map(team => team.name === result.losers.name
        ?
        { ...team,
          lost: team.lost + 1,
          goalsScored: team.goalsScored + result.losers.goals,
          goalsAgainst: team.goalsAgainst+ result.winners.goals
        }
        : team )
    }

当球队抽签时我需要另一个人。我也偶然发现了一个问题,每次我都会采取所有这些行动。即(如果一个团队赢了,我不需要调用UPDATE_IF_DRAW动作)但是当前的方式在调用动作后就可以了。

我想知道是否有人可以帮我写一个案例,我可以做一些像UPDATE_TABLE这样的事情。

我开始写这篇文章,但是当我意识到以下内容时,我发现了它,

团队A beats团队B)要求更新表的2个部分。即一个胜利和损失

这就是为什么我想要返回两件事,因为我想要映射我的状态,找到赢得并更新状态的团队并映射失去并更新状态的团队。

任何想法?

1 个答案:

答案 0 :(得分:0)

您可以使用地图功能中的条件检查来更新它们,如

case 'UPDATE_TABLE': {
      const result = workOutWinner(action.data.homeTeam, action.data.awayTeam)
      return state.map(team => team.name === result.losers.name
        ?
        { ...team,
          lost: team.lost + 1,
          goalsScored: team.goalsScored + result.losers.goals,
          goalsAgainst: team.goalsAgainst+ result.winners.goals
        }
        : team.name === result.winners.name ? 
          { ...team,
            won: team.won + 1,
            points: team.points + 3,
            goalsScored: team.goalsScored + result.winners.goals,
            goalsAgainst: team.goalsAgainst + result.losers.goals
          } : team )
    }