B
B
Barring2019-07-09 17:45:17
JavaScript
Barring, 2019-07-09 17:45:17

Options for writing a property to a component's state?

I'm going through the basic concepts of the React documentation and there I came across a similar class:

class
class Calculator extends React.Component {
  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});
  }

  render() {
    const scale = this.state.scale;
    const temperature = this.state.temperature;
    const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature;
    const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature;

    return (
      <div>
        <TemperatureInput
          scale="c"
          temperature={celsius}
          onTemperatureChange={this.handleCelsiusChange} />
        <TemperatureInput
          scale="f"
          temperature={fahrenheit}
          onTemperatureChange={this.handleFahrenheitChange} />
        <BoilingVerdict
          celsius={parseFloat(celsius)} />
      </div>
    );
  }
}

The class in the state has a temperature property initially equal to ''. In the future, as I understand it, it changes through the handleCelsiusChange and handleFahrenheitChange methods (the methods are given a number coming from other components as input). The principle of operation of both methods is the same, they set some value in scale - the property of the state, and I do not understand the next action, what is done with the temperature parameter? What does this record without assignment mean?
handleFahrenheitChange(temperature) {
    this.setState({scale: 'f', temperature});
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2019-07-09
@Barring

ES2015 Object Shorthand Properties

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question