1

This demo code is from react docs , you could search this sentence on the page:

this.setState({scale: 'c', temperature});
I thought the simply object {...} should has it's members form as key-value pair , Why can this demo just have a value as a member of the object ?
Mayank Shukla
  • 80,295
  • 14
  • 134
  • 129
frontGirl
  • 43
  • 8
  • When there is only the value, the key will automatically be the name of the variable, that is some es7 syntaxic sugar :) So in the end, you will have `{ scale: 'c', temperature: temperature }` – Icepickle Jun 04 '18 at 07:11

1 Answers1

2

With syntactic sugar on top of ES6 syntax, this.setState({scale: 'c', temperature}); is treated as

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

and hence it works as desired. This is an object property value shorthand syntax. You can read more about it here

Shubham Khatri
  • 211,155
  • 45
  • 305
  • 318