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

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

What is the meaning of

{...this.props}

I am trying to use it like that

 <div {...this.props}> Content Here </div>

ベストアンサー1

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={} />

So instead 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.

おすすめ記事