Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "...this.props" mean in ReactJS? [duplicate]

What does {...this.props} mean in this code?

<div {...this.props} style={{ height: `100%`, }}
like image 828
Joey Yi Zhao Avatar asked Apr 13 '16 06:04

Joey Yi Zhao


People also ask

What does this props mean in React?

“Props” is a special keyword in React, which stands for properties and is being used for passing data from one component to another. But the important part here is that data with props are being passed in a uni-directional flow. (

Why should you avoid copying the values of props into a component state?

Don't “copy props into state.” It creates a second source of truth for your data, which usually leads to bugs. One source of truth is best. Components will already re-render when their props change, so there's no need to duplicate the props as state and then try to keep it up to date.

What is the meaning of this in React?

When using 'this' in an object, this will refer to the object itself. This makes it easy to refer to an object's values in the object's methods.

Why props are immutable in React?

It allows passing data from one component to other components. It is similar to function arguments and can be passed to the component the same way as arguments passed in a function. Props are immutable so we cannot modify the props from inside the component.


1 Answers

The {...variable} syntax is called "spread attributes".

What this does is, basically, it takes every property of this.props (or any other passed variable) and applies them to the element.

Example:

props = {className: 'big', href: 'http://example.com'};

<a {...props} />
// the above line is equal to the following
<a className="big" href="http://example.com" />
like image 184
ROAL Avatar answered Oct 03 '22 14:10

ROAL