亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何重構那些非常相似的箭頭函數?

如何重構那些非常相似的箭頭函數?

慕無忌1623718 2021-10-07 10:29:48
我正在重構 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 語法中的某些內容來實現這一目標....
查看完整描述

1 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

只需switchEventSelectedRoundTable使用不執行任何操作的回調調用另一個函數即可。由于switchEventSelectedSchedulecurried,這很簡單:

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



查看完整回答
反對 回復 2021-10-07
  • 1 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號