Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do these 2 notations differ in meaning in React.js?

Came across these 2 notations recently. While both appear to be doing similar things, one of them uses an assignment operator while another one appears to be an object. Are they doing anything different?

const { value = "" } = this.props;

and

let { value: newValue } = newProps;
like image 692
internet Avatar asked Nov 25 '25 12:11

internet


1 Answers

It has nothing to do with React library actually. They are ES6 destructuring assignments.

The first example will create a constant variable called value and will assign whatever is this.props.value (Object, Array, Number). If this.props.value is undefined though it will use default value = "" - empty string.

const { value = "" } = this.props;

In a second example, newValue variable will be created and it will equal to newProps.value (even if it is undefined).

let { value: newValue } = newProps;
like image 124
Tomasz Mularczyk Avatar answered Nov 28 '25 00:11

Tomasz Mularczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!