1

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; 
1

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

3 Comments

So if I understand right it's the opposite of what I supposed and the same as const params = this.state.params; const code = this.state.code; const connection = this.state.connection;
Yes, exactly. Assignment operator works right to left even in JS.
Thanks! So this piece of code has no effect whatsoever, just call the function without the needless assign (since it are strings and copied by value)
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.