如何重构那些非常相似的箭头函数?

How to refactor those quite similar arrow functions?

我正在重构 React 文件中的一些代码,我有两个几乎做同样事情的函数...但是一个 return 一个函数,另一个执行一些代码。

我现在不太擅长 ES6 和箭头函数。而且我不明白如何重构它。

 switchEventSelectedSchedule = cb => option => {
    this.setState(
      // Mutate the state
      () => ({
        eventSelected: option.id,
        isLoading: true
      }),
      // Callback to fire when the state has been mutated with the new event id
      async () => {
        await this.longPollingAllMatches();
        this.setState(() => ({
          isLoading: false
        }));
        const { currentRoundId, rounds } = this.state;
        cb(rounds, currentRoundId);
      }
    );
  };

  switchEventSelectedRoundTable = option => {
    this.setState(
      // Mutate the state
      () => ({
        eventSelected: option.id,
        isLoading: true
      }),
      // Callback to fire when the state has been mutated with the new event id
      async () => {
        await this.longPollingAllMatches();
        this.setState(() => ({
          isLoading: false
        }));
      }
    );
  };

在一种情况下(假设 if(schedule))我需要 return cb 函数,否则我必须只执行其余代码。

抱歉看起来很愚蠢,但我想我误解了 ES6 语法中的一些东西来实现这个....

非常感谢!

只需 switchEventSelectedRoundTable 使用不执行任何操作的回调调用另一个函数。由于 switchEventSelectedSchedulecurried,这很简单:

switchEventSelectedRoundTable = switchEventSelectedSchedule((rounds, currentRoundId) => {});