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?