1

Going through React, I was wondering why the method handlechange wasn't coded as the following. As I've tried both seem to work. Wondering if this is a destructuring assignement.

 handleCelsiusChange(temperature) {
    this.setState({scale: 'c', temperature: temperature});
  }

original code:

 constructor(props) {
    super(props);
    this.handleCelsiusChange = this.handleCelsiusChange.bind(this);
    this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
    this.state = {temperature: '', scale: 'c'};
  }

  handleCelsiusChange(temperature) {
    this.setState({scale: 'c', temperature});
  }

  handleFahrenheitChange(temperature) {
    this.setState({scale: 'f', temperature});
  }

Code takend from reactJs

Brandon
  • 315
  • 1
  • 9
  • 2
    No, it's not... Just passing an object as an argument to a function. – Robby Cornelissen Jan 30 '20 at 06:33
  • are you referring to changing `temperature: temperature` to just `temperature`? That's not desctructuring it's [short-hand property names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Method_definitions) – Nick Parsons Jan 30 '20 at 06:49

1 Answers1

1

The difference between

 handleCelsiusChange(temperature) {
    this.setState({scale: 'c', temperature: temperature});
  }

and

  handleCelsiusChange(temperature) {
    this.setState({scale: 'c', temperature});
  }

is that second use shorter notation which became available in ECMAScript 2015. You may see description with samples here

Fyodor
  • 7,344
  • 1
  • 20
  • 30