Everytime I define a handler method, I am forced to repeat variables even if two methods share the same ones. For instance:
handleBreak(evt) {
const id = evt.target.id;
let breakVar = this.state.breakSession;
let isPaused = this.state.isPaused;
let breakBegan = this.state.breakBegan;
}
handleSession(evt) {
const id = evt.target.id;
let sessionVar = this.state.session;
let isPaused = this.state.isPaused;
let intervalBegan = this.state.intervalBegan;
let breakBegins = document.getElementById('timer-label');
let breakBegan = this.state.breakBegan;
}
Aside from creating a function that contains those same variables and passing the return to the handlers, is there a cleaner way to do this?
You can use ES6 Destructuring and do something like
const { session, isPaused, intervalBegan, breakBegan } = this.state;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With