最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react.js setState call with key but without value? - Stack Overflow

programmeradmin4浏览0评论

Just started to learn react.js and javascript. I'm going through all the documentation on facebook's github, but got stuck with this.

In the handleCelsiusChange method of Calculator class in Lifting state up chapter there is this line:

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

So scale will get the value 'c'. Okay. But what is this value being simply there? Shouldn't it be a key-value pair? I've checked the explanation of setState():

The first argument can be an object (containing zero or more keys to update) or a function (of state and props) that returns an object containing keys to update.

But it says nothing relevant about this usage.

Thanks! :)

Just started to learn react.js and javascript. I'm going through all the documentation on facebook's github, but got stuck with this.

In the handleCelsiusChange method of Calculator class in Lifting state up chapter there is this line:

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

So scale will get the value 'c'. Okay. But what is this value being simply there? Shouldn't it be a key-value pair? I've checked the explanation of setState():

The first argument can be an object (containing zero or more keys to update) or a function (of state and props) that returns an object containing keys to update.

But it says nothing relevant about this usage.

Thanks! :)

Share Improve this question edited Feb 11, 2017 at 22:34 inspiral asked Feb 11, 2017 at 22:18 inspiralinspiral 6811 gold badge9 silver badges17 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 16

That's actually a feature of ES6. If the key matches an existing variable name you can use this shorthand syntax. So instead of writing value: value you can simply write value as key and variable name are the same.

Example with ES6

function getCar(make, model, value) {
    return {
        // with property value shorthand
        // syntax, you can omit the property
        // value if key matches variable
        // name
        make,
        model,
        value
    };
}

The equivalent of the above in ES3/ES5

function getCar(make, model, value) {
    return {
        make: make,
        model: model,
        value: value
    };
}

Example taken from http://www.benmvp./learning-es6-enhanced-object-literals/

That is a special shorthand notation from ES6, it means value: value, look here https://ariya.io/2013/02/es6-and-object-literal-property-value-shorthand for more details

发布评论

评论列表(0)

  1. 暂无评论