I recently followed a react tutorial to create a template project and have since been modifying code to fit my needs. In particular, there was this piece of code on a component that was passed a parameter called label
.
render() {
const { label } = this.props;
...
}
In this example, I returned a JSON object from a controller and passed it to this component using a parameter named rune
. A property of the rune
JSON is "name", and I wanted to assign the name to a variable called `label. One thing that gave me trouble was the following:
render() {
console.log("Prop.runes.name: " + this.props.rune.name);
const { label } = this.props.rune.name;
console.log("Label: " + label);
...
}
The first console.log(...)
outputs the name correctly. However, the second log was undefined. After some trial and error, I found that if I removed the curly braces from my const
declaration the name resolved properly.
render() {
const label = this.props.rune.name;
...
}
What were the curly braces originally doing? Is there a reason the tutorial initially had them?
From documentation The exterior set of curly braces are letting JSX know you want a JS expression. The interior set of curly braces represent a JavaScript object, meaning you're passing in a object to the style attribute.
You put curly braces when you want to use the value of a variable inside "html" (so inside the render part). It's just a way of telling the app to take the value of the variable and put it there, as opposed to a word.
With JSX you can write expressions inside curly braces { } . The expression can be a React variable, or property, or any other valid JavaScript expression.
Declare a template in the HTML file. Handlebars expressions are put into double curly braces {{expr}} for HTML-escaped content; otherwise, use triple curly brackets {{{expr}}} to avoid HTML-escaping.
What you ask here is not related to React actually, it is related to Javascript: Destructuring assignment.
For objects, you can destruct a property like that:
const obj = {
name: "foo",
};
const { name } = obj;
console.log( name );
const name2 = obj.name;
console.log( name2 );
Above, both assignments are equal. First one is the shorthand of the second one.
For your example:
const { label } = this.props.rune.name;
Here, there is a this.props.rune.name.label
property and you are destructing it from this.props.rune.name
. This is equal to:
const label = this.props.rune.name.label;
not actually
const label = this.props.rune.name;
as you tried.
The related part of React with this syntax is we see it very frequently in React world. Like:
render() {
const { foo, bar } = this.props;
const { fizz, buzz } = this.state;
return (
<p>A {foo} goes to {bar} and orders a {fizz} without {buzz}</p>;
)
}
or
const AComponent = ( { foo, bar } ) => (
<p>{foo} loves {bar} in programming world.</p>
);
A caution. When dealing with nested properties to destruct being careful is important. Since as @Tyler Sebastian explained in the comments it is not null
safe.
const data = { obj: { name : "foo"} };
const { obj: { name } } = data;
This is OK but if we have a typo there or if we try to use a property which does not exist at that time we get an error
since we try to destruct a property from an undefined
one.
const data = { obj: { name : "foo"} };
const { objx: { name } } = data;
This throws an error. Thanks to @Tyler Sebastian for the comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With