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.