Thomas Stubbe
Thomas Stubbe

Reputation: 2005

What is the effect of const object = this.variable?

I'm working on a react-native app which was initially developed by a remote company. In this project I found the following code:

class SomeScreen extends Component {

  constructor {
    this.state = {
      connection: null
      codeInput: '',
      paramInput: '',
    }
  }

  someFunction() {
    const {
      connection,
      codeInput: code,
      paramInput: params,
    } = this.state
    this.otherFunction(connetion.var, parseInt(code), parseInt(params))
  }
}

I'm wondering what the const {} = this.variable does. I've never seen this way of assigning and I'm wondering if it's the same as

this.state.code = code;
this.state.params = params;

Upvotes: 1

Views: 138

Answers (2)

Murat Taştemel
Murat Taştemel

Reputation: 11

It is Object Destructuring. Therewith you can unpack from values from arrays or properties from object. You can click link for more detailed information Object Destructuring Info

Upvotes: 1

Alex T
Alex T

Reputation: 1366

Consider the code snippet below to understand your code. Basically you are just making a copy to a const variable.

let obj = {a:1, b:2, c:3}
const {a} = obj;
console.log(a);
obj.a = 4;
console.log(obj.a);
console.log(a);

Upvotes: 2

Related Questions