我正在重構(gòu) react 文件中的一些代碼,我有兩個函數(shù)幾乎可以做同樣的事情……但是一個返回一個函數(shù),另一個執(zhí)行一些代碼。我現(xiàn)在不太擅長 ES6 和箭頭功能。我不明白如何重構(gòu)它。 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 函數(shù),否則我必須只執(zhí)行其余的代碼。抱歉似乎很愚蠢,但我想我誤解了 ES6 語法中的某些內(nèi)容來實現(xiàn)這一目標....
如何重構(gòu)那些非常相似的箭頭函數(shù)?
慕無忌1623718
2021-10-07 10:29:48