@danilr

Почему тут запись в setState не в стандартном синтаксисе?

Здесьthis.setState({scale: 'c', temperature}); temperature странно задается, почему так?
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) {
    console.log(temperature)
    this.setState({scale: 'f', temperature});
  }
  • Вопрос задан
  • 73 просмотра
Решения вопроса 2
BRAGA96
@BRAGA96
ES6. Это значит temperature ключ и значение из переменной temperature, то есть { temperature: temperature } на ES5
const test = true
const obj = { test } //> { test: true }
Ответ написан
Комментировать
rockon404
@rockon404 Куратор тега React
Frontend Developer
Object property shorthand

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

равносильно:
this.setState({ scale: 'c', temperature: temperature });
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы