158

What is the meaning of

{...this.props}

I am trying to use it like that

 <div {...this.props}> Content Here </div>
Swaraj Ghosh
  • 2,284
  • 3
  • 16
  • 19

7 Answers7

252

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.

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
  • 4
    Just to add, it can help to think of it as a replacement to `this.transferPropsTo` which was deprecated in React 0.12.x and will be removed in 0.13.x. It of course allows much more advanced usage however simply translating React 0.11.x's `this.transferPropsTo()` to `` is most useful for people making that transition. – Mike Driver Feb 11 '15 at 14:53
  • 22
    Good awnser, but 'you "never" use {... props} inside your render() function, only when you pass the props down to another component.' Is a very confusing term of phrase. Recommend to rewrite is as "You only use {... props} inside your render() when you pass the props down to another component." for clarity. – Dani Oct 12 '17 at 21:14
26

It's ES6 Spread_operator and Destructuring_assignment.

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

It's equal to Class Component

const person = {
    name: "xgqfrms",
    age: 23,
    country: "China"
};

class TestDemo extends React.Component {
    render() {
        const {name, age, country} = {...this.props};
        // const {name, age, country} = this.props;
        return (
          <div>
              <h3> Person Information: </h3>
              <ul>
                <li>name={name}</li>
                <li>age={age}</li>
                <li>country={country}</li>
              </ul>
          </div>
        );
    }
}

ReactDOM.render(
    <TestDemo {...person}/>
    , mountNode
);

enter image description here


or Function component

const props = {
    name: "xgqfrms",
    age: 23,
    country: "China"
};

const Test = (props) => {
  return(
    <div
        name={props.name}
        age={props.age}
        country={props.country}>
        Content Here
        <ul>
          <li>name={props.name}</li>
          <li>age={props.age}</li>
          <li>country={props.country}</li>
        </ul>
    </div>
  );
};

ReactDOM.render(
    <div>
        <Test {...props}/>
        <hr/>
        <Test 
            name={props.name}
            age={props.age}
            country={props.country}
        />
    </div>
    , mountNode
);

enter image description here

refs

xgqfrms
  • 10,077
  • 1
  • 69
  • 68
2

It is ES-6 feature. It means you extract all the properties of props in div.{... }

operator is used to extract properties of an object.

roottraveller
  • 7,942
  • 7
  • 60
  • 65
2

It will compile to this:

React.createElement('div', this.props, 'Content Here');

As you can see above, it passes all it's props to the div.

Vad
  • 4,052
  • 3
  • 29
  • 34
2

You will use props in your child component

for example

if your now component props is

{
   booking: 4,
   isDisable: false
}

you can use this props in your child compoenet

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

in you child component, you will receive all your parent props.

2

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

0

we can pass props for example in nextJS with :

            <Component {...{
              props,
              allCount: count?.lenght,
            }} />
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 22 '22 at 21:27