在reactjs中分配状态常量

时间:2018-01-04 10:20:34

标签: javascript reactjs

这里编写代码的方法是什么,我们说

const {
         month,
        } = this.state;

在这个特定的代码块中?

previous() {
        const {
          month,
        } = this.state;

        this.setState({
          month: month.subtract(1, 'month'),
        });
      }

3 个答案:

答案 0 :(得分:1)

这称为解构分配(ES6特征)。

<强> ES6

const { month } = this.state;

<强> ES5

const month =  this.state.month;

您可以从http://es6-features.org/#ObjectAndArrayMatchingDefaultValues

获取更多详细信息

答案 1 :(得分:0)

它被称为解构。您可以找到详细信息here

答案 2 :(得分:0)

es6中的解构分配:

let obj = {key1 : "string", key2: ()=>'function'};
const {key1, key2} = obj;
console.log(key1) // returns 'string'
console.log(key2()) // returns 'function'