Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of {...this.props} in Reactjs

Tags:

reactjs

What is the meaning of

{...this.props}

I am trying to use it like that

 <div {...this.props}> Content Here </div>
like image 584
Swaraj Ghosh Avatar asked Oct 05 '22 16:10

Swaraj Ghosh


1 Answers

It's called spread attributes and its aim is to make the passing of props easier.

Let us imagine that you have a component that accepts N number of properties. Passing these down can be tedious and unwieldy if the number grows.

<Component x={} y={} z={} />

Thus instead you do this, wrap them up in an object and use the spread notation

var props = { x: 1, y: 1, z:1 };
<Component {...props} />

which will unpack it into the props on your component, i.e., you "never" use {... props} inside your render() function, only when you pass the props down to another component. Use your unpacked props as normal this.props.x.

like image 149
Henrik Andersson Avatar answered Oct 08 '22 04:10

Henrik Andersson