I'm refactoring some code in a react file, and I have two functions which almost does the same thing... but one return a function and the other one execute some code.
I'm not really good for now with ES6 and arrow function. And I don't understand how to refactor this.
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
}));
}
);
};
in one case (imagine if(schedule)) I need to return the cb function else I must just execute the rest of the code.
Sorry seems dumb but I think I misunderstood something in the ES6 syntax to achieve this....
Many thx !