Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forced to repeat variables in methods

Tags:

reactjs

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?

like image 207
Hernan Ariel Avatar asked Apr 17 '26 20:04

Hernan Ariel


1 Answers

You can use ES6 Destructuring and do something like

const { session, isPaused, intervalBegan, breakBegan } = this.state;
like image 52
James Avatar answered Apr 19 '26 16:04

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!