我正在重構 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 語法中的某些內容來實現這一目標....
如何重構那些非常相似的箭頭函數?
慕無忌1623718
2021-10-07 10:29:48