我正在重构 react 文件中的一些代码,我有两个函数几乎可以做同样的事情……但是一个返回一个函数,另一个执行一些代码。我现在不太擅长 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))我需要返回 cb 函数,否则我必须只执行其余的代码。抱歉似乎很愚蠢,但我想我误解了 ES6 语法中的某些内容来实现这一目标....
添加回答
举报
0/150
提交
取消