How to fix Eslint error “prefer-destructuring”?

前端 未结 3 1538
悲&欢浪女
悲&欢浪女 2020-12-08 18:57

I wanted to shorten an object literal in ES6 like this:

const loc = this.props.local;

The reason is loc.foo(); is a lot easier

相关标签:
3条回答
  • 2020-12-08 19:08

    It's a new construct in ES 6 that allows you to match property of an object in assignment. The syntax you need is:

    const { local: loc } = this.props
    

    which translates to: "declare a constant loc and assign it the value of property local from this.props".

    0 讨论(0)
  • 2020-12-08 19:08

    It's telling you to use

    const {props: {local: loc}} = this;
    
    0 讨论(0)
  • 2020-12-08 19:14

    change your code from:

    const local = this.props.local;
    

    to:

    const { local } = this.props;
    

    They are equivalent and you can call local.foo() in the same way. except that the second use object destructuring.

    0 讨论(0)
提交回复
热议问题