Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: var {left, ...props} = this.props;

I was reading the source code of Facebook's fixed-data-table, and i found this

var {left, ...props} = this.props;

What that it means? is this a new semantic? I'm confused o.O

like image 803
Spike886 Avatar asked Feb 16 '15 04:02

Spike886


People also ask

What is this props mean?

this.props is the object which contains the Attributes which we have passed to the parent component.

How do I pass Props to JSX element?

To pass props, add them to the JSX, just like you would with HTML attributes. To read props, use the function Avatar({ person, size }) destructuring syntax. You can specify a default value like size = 100 , which is used for missing and undefined props.

How do you access props in attribute quotes?

Following approaches are there to access props inside quotes in React JSX: Approach 1: We can put any JS expression inside curly braces as the entire attribute value. Approach 2: We can use ES6 template literals.

Why we use this props in ReactJS?

We use props in React to pass data from one component to another (from a parent component to a child component(s)). Props is just a shorter way of saying properties. They are useful when you want the flow of data in your app to be dynamic.


1 Answers

It's a special form of destructuring assignment proposed for ES7 (and eagerly implemented in the jsx tools and Babel). It creates two variables: left, and props.

left has the value of this.props.left.

props is an object with all of the other properties of this.props (excluding left).

If you wrote it without destructuring it'd look like this:

var left = this.props.left;
var props = {};

Object.keys(this.props).forEach(function(key, index){
    if (key !== 'left') {
        props[key] = this.props[key];
    }
}, this);

That's more than a few characters shaved off :-)

like image 50
Brigand Avatar answered Oct 24 '22 00:10

Brigand