Redux const { var1, var2 } = this.props; [duplicate] - javascript

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
children prop in React component
(3 answers)
beginner's: const definition in Redux confusing
(1 answer)
Closed 5 years ago.
I am new to react js. I am working on a project and found this line
const { var1, var2 } = this.props;
Props which are comming in this component are
type PropsType = {
var1: Array<any>,
a: Array<any>,
b: boolean,
c: boolean,
var2: (string) => void,
d: () => void,
e: () => void
};
I am confused. What it means?

const { var1, var2 } = this.props;
// the same as
// const var1 = this.props.var1;
// const var2 = this.props.var2;
Have you tried to read the docs ?

Well redundant information, but a revision for me. :)
Anyways to start with, this is destructuring object assignment. What this means is that, this is a shorthand way to get the object properties' value from an object(such as this.props in here).
So when you want to extract a property named 'var1' and 'var2' from the 'this.props', by writing the instruction -
const { var1, var2 } = this.props;
you ask for the property named 'var1' and 'var2' from 'this.props' to be stored in constants 'var1' and 'var2'. All other properties are simply ignored. And if any of the asked property names are not there, they are simply given 'unassigned' value.
After this, you may consider going through more details(magic of it!) here -
MDN - object_destructuring

This is not directly related to React nor Redux. It is ES6 Object Destructuring assignment.
It means that you're assigning this.props.var1 and this.props.var2 to consts var1 and var2 respectively.

Related

How the square bracket is being used inside an object [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 2 months ago.
const handleChange = (event) => {
const { name, value } = event.target;
Here is [name] that is inside the square bracket. I know what it's doing but I just want to know what was the logic under the hood to use this logic?
setFormFields({ ...formFields, [name]: value });
console.log(formFields);
};
If it's array destructuring why and how is it working and why not object destructuring and what is the reason behind using this logic?
In your first example, const { name, value } = event.target; is object destructuring, pulling the name and value properties off of event.target;
While your second example looks the same, it's the opposite - instead of destructuring an object into variables you're creating a new object. Somewhere above that line, there's a variable called name.
You can think of the example like this:
const name = 'someKey';
const value = 'someValue';
const formFields = { keyOne: 'one' };
const newObject = { ...formFields, [name]: value }; // { keyOne: 'one', someKey: 'someValue' };

Destructure property whose name is a reserverd keyword [duplicate]

This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
Closed 3 years ago.
While using material ui, I realized they have a prop called in in the transitions components, but when trying to destruct the props, I can't because in is a reserved key word.
const MyFade = ({ children, in, ...otherProps }) => { // this gives me an error
return (
<div {...otherProps}>
<Fade in={in}>{children}</Fade>
</div>
);
};
How can I do this? I need to destruct in and have otherProps to spread in the div.
Just assign a new, not reserved name inside destructuring.
const o = {
in: 'foo',
out: 'boo',
};
const { in: inProp } = o;
// ^^^^ assign new name
console.log(inProp);

const {name, value} = event.target -- what does this mean [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Syntax: const {} = variableName, can anyone explain or point me into the right direction [duplicate]
(1 answer)
Closed 3 years ago.
handleChange(event) {
const {name, value} = event.target
this.setState({
[name]: value
})
}
this is a method that notes for change in state of a react component. takes event as parameter and does something and changes state.
This method is called destructuring, which is used to save some few lines.
The following example will show you the usage of destructuring.
let person = {
name: 'David',
age: 15,
job: 'Programmer'
}
const { name, age } = person; // Takes the property/method from the object
console.log(name); // Prints 'David'
console.log(age); // Prints '15'
Without using destructuring, I would have done this:
const name = person.name;
const age = person.age;
Which needed more lines.
You can read more about destructuring here.
event.target means element where you pass the function, and { name } this technique is called destructing it is same as const name = event.target.name
for example if you pass handleChange to some input event.target is input where you passed handleChange function

What does this line of code mean? const {"intl": { formatMessage }, } = this.context [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 4 years ago.
I'm new in react and I'm reading some code i found this line of code:
const {intl: { formatMessage }, } = this.context
is a const declaration but i don't understand
I know is JS ES6 but i don't understand what is it for?
How can I check the value of this const?
thanks
As people already answered. This is object destructuring
Simple example: const contact = {name: 'John', email: 'john#doe.com'}
With ES6 you can do const {email} = contact; //email = contact.email
In case you want to name the variable differently, it would be:
const {email: mailbox} = contact //equivalent to mailbox = contact.email;
Back to the original question: {intl: { formatMessage }, } = this.context
=> {formatMessage} = this.context.intl => formatMessage = this.context.intl.formatMessage
The code that you have is, actually a representation of the following code,
const formatMessage = this.context.intl.formatMessage
You can read about object destructuring to know more about it.
This simply means that this.context contains a structure similar to this
this.context = {
intl:{
formatMessage: // This has a value let's say "Hello"
},
//Other key-value pairs could be contained in the context object }
This line of code is a shorthand syntax telling you that only the formatMessage property which can be found inside "intl" should be retrieved from the large object called "this.context"

Weird javascript code convention with "const" [duplicate]

This question already has answers here:
beginner's: const definition in Redux confusing
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
Closed 5 years ago.
What is this code convention in javascript?
const { navigate } = //whatever
As in, what sense does it make. I saw it in RNs React navigation
https://reactnavigation.org/docs/intro/
It's named destructuring. When you have an object and you want to take only a property of that object, you can get only it by using that convention.
let fullName = {
first: 'John',
last: 'Smith'
}
const { first } = fullName;
You can take a look here for more info's
http://wesbos.com/destructuring-renaming/
It's called destructuring
Example:
var myObject = {a: "what", b: "ever"};
const {a} = myObject;
console.log(a); // will give "what"

Categories